em = $em; $this->configRepository = $configRepository; $this->chargeLabels = $configRepository->getChargesLabels(); $this->resourcesLabels = $configRepository->getResourcesLabels(); $this->translatableStringHelper = $translatableStringHelper; } public function getSummaryForHousehold(?Household $household): array { if (null === $household) { return [ 'resources' => $this->getEmptyResourceArray(), 'charges' => $this->getEmptyChargeArray(), ]; } $personIds = $household->getCurrentPersons()->map(static function (Person $p) { return $p->getId(); }); $ids = implode(', ', array_fill(0, count($personIds), '?')); $parameters = [...$personIds, $household->getId()]; $rsm = $this->buildRsm(); $resources = $this->em->createNativeQuery(strtr(self::QUERY_RESOURCE_BY_HOUSEHOLD, ['_ids_' => $ids]), $rsm) ->setParameters($parameters) ->getResult(); $charges = $this->em->createNativeQuery(strtr(self::QUERY_CHARGE_BY_HOUSEHOLD, ['_ids_' => $ids]), $rsm) ->setParameters($parameters) ->getResult(); return [ 'resources' => array_merge($this->getEmptyResourceArray(), $this->rowToArray($resources, 'resource')), 'charges' => array_merge($this->getEmptyChargeArray(), $this->rowToArray($charges, 'charge')), ]; } public function getSummaryForPerson(?Person $person): array { if (null === $person) { return [ 'resources' => $this->getEmptyResourceArray(), 'charges' => $this->getEmptyChargeArray(), ]; } $rsm = $this->buildRsm(); $resources = $this->em->createNativeQuery(self::QUERY_RESOURCE_BY_PERSON, $rsm) ->setParameters([$person->getId()]) ->getResult(); $charges = $this->em->createNativeQuery(self::QUERY_CHARGE_BY_PERSON, $rsm) ->setParameters([$person->getId()]) ->getResult(); return [ 'resources' => array_merge($this->getEmptyResourceArray(), $this->rowToArray($resources, 'resource')), 'charges' => array_merge($this->getEmptyChargeArray(), $this->rowToArray($charges, 'charge')), ]; } private function buildRsm(): ResultSetMapping { $rsm = new ResultSetMapping(); $rsm ->addScalarResult('sum', 'sum') ->addScalarResult('type', 'type'); return $rsm; } private function getEmptyChargeArray(): array { $keys = $this->configRepository->getChargesKeys(); $labels = $this->chargeLabels; return array_combine($keys, array_map(function ($i) use ($labels) { return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($labels[$i])]; }, $keys)); } private function getEmptyResourceArray(): array { $keys = $this->configRepository->getResourcesKeys(); $labels = $this->resourcesLabels; return array_combine($keys, array_map(function ($i) use ($labels) { return ['sum' => 0.0, 'label' => $this->translatableStringHelper->localize($labels[$i])]; }, $keys)); } private function rowToArray(array $rows, string $kind): array { switch ($kind) { case 'charge': $label = $this->chargeLabels; break; case 'resource': $label = $this->resourcesLabels; break; default: throw new LogicException(); } $result = []; foreach ($rows as $row) { $result[$row['type']] = [ 'sum' => (float) $row['sum'], 'label' => $this->translatableStringHelper->localize($label[$row['type']]), ]; } return $result; } }