public string ApplyEmployeeIdentities(string response, IdentityCache identityCache)
        {
            var xmlResponse = new XmlDocument();
            xmlResponse.LoadXml(response);

            var validRequestTypes = new List<RequestType> {RequestType.P6, RequestType.P9, RequestType.SL1, RequestType.SL2};
            var requestType = requestTypeResolver.GetRequestTypeForResponse(xmlResponse);

            if (!validRequestTypes.Contains(requestType))
                return response;

            switch (requestType)
            {
                case RequestType.P6:
                    UpdateIdentities(xmlResponse, "CodingNoticesP6P6B", identityCache);
                    break;
                case RequestType.P9:
                    UpdateIdentities(xmlResponse, "CodingNoticeP9", identityCache);
                    break;
                case RequestType.SL1:
                    UpdateIdentities(xmlResponse, "StudentLoanStart", identityCache);
                    break;
                case RequestType.SL2:
                    UpdateIdentities(xmlResponse, "StudentLoanEnd", identityCache);
                    break;
            }

            return GetXmlDocumentAsString(xmlResponse);
        }
        public HttpResponseMessage GetMessageResponseFor(HttpRequestMessage request, IdentityCache identityCache)
        {
            var requestContent = request.Content.ReadAsStringAsync().Result;
            var configuration = configRepository.GetConfiguration();

            var result = messageSender.PostXml(requestContent, configuration.HmrcGetMessagesServer);

            var response = result.Response;

            if (identityCache != null)
                response = dataManipulator.ApplyEmployeeIdentities(response, identityCache);

            return new HttpResponseMessage
            {
                Content = new StringContent(response, Encoding.UTF8)
            };
        }
        private EmployeeIdentity GetNextFreeIdentity(List<EmployeeIdentity> identities, IdentityCache identityCaches)
        {
            var ids = (from identity in identities
                       join cachedIdentities in identityCaches.Identities.Values on identity.EmployeePayId equals cachedIdentities.EmployeePayId into iug
                       from idUsage in iug.DefaultIfEmpty()
                       select new
                       {
                           Identity = identity,
                           Usages = iug.Count()
                       }).ToList();

            return ids.OrderBy(x => x.Usages).ThenBy(x => x.Identity.EmployeePayId).Select(x => x.Identity).FirstOrDefault();
        }
        private void UpdateIdentities(XmlDocument xmlResponse, string messageNodeName, IdentityCache identityCache)
        {
            var identities = repository.Get().ToList();
            var nodes = xmlResponse.GetElementsByTagName(messageNodeName);

            foreach (XmlNode messageNode in nodes)
            {
                var messageNino = GetNationalInsuranceNoFromMessage(messageNode);
                var cacheIdentity = identityCache.Identities.Where(x => x.Key == messageNino).Select(x => x.Value).FirstOrDefault();
                if (cacheIdentity == null)
                {
                    cacheIdentity = GetNextFreeIdentity(identities, identityCache);
                    if (cacheIdentity == null)
                        continue;

                    identityCache.Identities.Add(messageNino, cacheIdentity);
                }

                ApplyIdentityToMessage(messageNode, cacheIdentity);
            }
        }