Lines
81.15%
56 / 69
Functions and Methods
50.00%
2 / 4
Classes and Traits
0.00%
0 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Calldisplay | 81.15% | 56 / 69 | 50.00% | 2 / 4 | 23.95 | 0.00% | 0 / 1 | |||
| readResolvedEntity | 72.41% | 21 / 29 | 0.00% | 0 / 1 | 8.03 | |||||
| readResolvedOrganisation | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 3 | |||||
| readImage | 73.68% | 14 / 19 | 0.00% | 0 / 1 | 5.46 | |||||
| readContactData | 100.00% | 12 / 12 | 100.00% | 1 / 1 | 6 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace BO\Zmsbackend\Calldisplay\Service; | |
| 4 | ||
| 5 | use BO\Zmsentities\Calldisplay as Entity; | |
| 6 | ||
| 7 | /** | |
| 8 | * | |
| 9 | * @SuppressWarnings(CouplingBetweenObjects) | |
| 10 | */ | |
| 11 | class Calldisplay extends \BO\Zmsbackend\Base | |
| 12 | { | |
| 13 | /** | |
| 14 | * read calldisplay with resolved scopes and clusters | |
| 15 | * | |
| 16 | * @param Entity $calldisplay | |
| 17 | * @param \DateTimeImmutable $dateTime | |
| 18 | * @param int $resolveReferences | |
| 19 | * | |
| 20 | * @return Entity | |
| 21 | */ | |
| 22 | public function readResolvedEntity(Entity $calldisplay, \DateTimeImmutable $dateTime, int $resolveReferences = 0) | |
| 23 | { | |
| 24 | if ($calldisplay->hasScopeList()) { | |
| 25 | $scopeList = new \BO\Zmsentities\Collection\ScopeList(); | |
| 26 | foreach ($calldisplay->scopes as $entity) { | |
| 27 | $query = new \BO\Zmsbackend\Scope\Service\Scope(); | |
| 28 | $scope = $query->readEntity($entity['id'], $resolveReferences - 1); | |
| 29 | if (! $scope) { | |
| 30 | \App::$log->warning('Calldisplay: skip missing scope id while resolving', [ | |
| 31 | 'scopeId' => $entity['id'], | |
| 32 | ]); | |
| 33 | continue; | |
| 34 | } | |
| 35 | $scopeList->addEntity($scope); | |
| 36 | } | |
| 37 | $calldisplay->scopes = $scopeList; | |
| 38 | } | |
| 39 | if ($calldisplay->hasClusterList()) { | |
| 40 | $clusterList = new \BO\Zmsentities\Collection\ClusterList(); | |
| 41 | foreach ($calldisplay->clusters as $entity) { | |
| 42 | $query = new \BO\Zmsbackend\Cluster\Service\Cluster(); | |
| 43 | $cluster = $query->readEntity($entity['id'], $resolveReferences); | |
| 44 | if (! $cluster) { | |
| 45 | \App::$log->warning('Calldisplay: skip missing cluster id while resolving', [ | |
| 46 | 'clusterId' => $entity['id'], | |
| 47 | ]); | |
| 48 | continue; | |
| 49 | } | |
| 50 | $clusterList->addEntity($cluster); | |
| 51 | } | |
| 52 | $calldisplay->clusters = $clusterList; | |
| 53 | } | |
| 54 | $calldisplay->setServerTime($dateTime->getTimestamp()); | |
| 55 | $calldisplay->organisation = $this->readResolvedOrganisation($calldisplay); | |
| 56 | $calldisplay->image = $this->readImage($calldisplay); | |
| 57 | $calldisplay->contact = $this->readContactData($calldisplay); | |
| 58 | return $calldisplay->withOutClusterDuplicates(); | |
| 59 | } | |
| 60 | ||
| 61 | public function readResolvedOrganisation(Entity $entity) | |
| 62 | { | |
| 63 | $organisation = null; | |
| 64 | $query = new \BO\Zmsbackend\Organisation\Service\Organisation(); | |
| 65 | $scope = $entity->getScopeList()->getFirst(); | |
| 66 | $cluster = $entity->getClusterList()->getFirst(); | |
| 67 | if ($scope) { | |
| 68 | $organisation = $query->readByScopeId($scope->id); | |
| 69 | } elseif ($cluster) { | |
| 70 | $organisation = $query->readByClusterId($cluster->id); | |
| 71 | } | |
| 72 | return $organisation; | |
| 73 | } | |
| 74 | ||
| 75 | /** | |
| 76 | * @return (mixed|string)[] | |
| 77 | * | |
| 78 | */ | |
| 79 | public function readImage(Entity $entity): array | |
| 80 | { | |
| 81 | $name = $entity->getImageName(); | |
| 82 | $image = null; | |
| 83 | if ($name) { | |
| 84 | $image = $this->getReader() | |
| 85 | ->fetchOne((new \BO\Zmsbackend\Calldisplay\Repository\Calldisplay(\BO\Zmsbackend\Query\Base::SELECT)) | |
| 86 | ->getQueryImage(), ['name' => "$name%"]); | |
| 87 | } | |
| 88 | if (! $image) { | |
| 89 | $image = $this->getReader() | |
| 90 | ->fetchOne((new \BO\Zmsbackend\Calldisplay\Repository\Calldisplay(\BO\Zmsbackend\Query\Base::SELECT)) | |
| 91 | ->getQueryImage(), ['name' => "logo.png"]); | |
| 92 | } | |
| 93 | ||
| 94 | if (! is_array($image)) { | |
| 95 | return [ | |
| 96 | 'name' => '', | |
| 97 | 'data' => '', | |
| 98 | 'mime' => '', | |
| 99 | ]; | |
| 100 | } | |
| 101 | ||
| 102 | $mime = pathinfo($image['name'] ?? '', PATHINFO_EXTENSION); | |
| 103 | $image['mime'] = ($mime == 'jpg') ? 'jpeg' : $mime; | |
| 104 | return $image; | |
| 105 | } | |
| 106 | ||
| 107 | public function readContactData(Entity $entity): \BO\Zmsentities\Contact | |
| 108 | { | |
| 109 | $contact = new \BO\Zmsentities\Contact(); | |
| 110 | $contactNames = []; | |
| 111 | if ($entity->hasClusterList()) { | |
| 112 | foreach ($entity->getClusterList() as $cluster) { | |
| 113 | $contactNames[] = $cluster->name; | |
| 114 | } | |
| 115 | } elseif ($entity->hasScopeList()) { | |
| 116 | foreach ($entity->getScopeList() as $scope) { | |
| 117 | $department = (new \BO\Zmsbackend\Department\Service\Department())->readByScopeId($scope->id); | |
| 118 | $contactNames[] = $department->name; | |
| 119 | } | |
| 120 | } | |
| 121 | ||
| 122 | $contactNames = array_unique($contactNames); | |
| 123 | $contact->name = count($contactNames) > 1 ? '' : $contactNames[0]; | |
| 124 | ||
| 125 | return $contact; | |
| 126 | } | |
| 127 | } |