Lines
100.00%
88 / 88
Functions and Methods
100.00%
9 / 9
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| RequestRelation | 100.00% | 88 / 88 | 100.00% | 9 / 9 | 22 | 100.00% | 1 / 1 | |||
| readEntity | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 1 | |||||
| readListBySource | 100.00% | 9 / 9 | 100.00% | 1 / 1 | 1 | |||||
| readListByRequestId | 100.00% | 10 / 10 | 100.00% | 1 / 1 | 1 | |||||
| readListByProviderId | 100.00% | 10 / 10 | 100.00% | 1 / 1 | 1 | |||||
| writeListBySource | 100.00% | 16 / 16 | 100.00% | 1 / 1 | 6 | |||||
| writeEntity | 100.00% | 11 / 11 | 100.00% | 1 / 1 | 2 | |||||
| writeImportList | 100.00% | 15 / 15 | 100.00% | 1 / 1 | 7 | |||||
| readList | 100.00% | 5 / 5 | 100.00% | 1 / 1 | 2 | |||||
| writeDeleteListBySource | 100.00% | 3 / 3 | 100.00% | 1 / 1 | 1 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | namespace BO\Zmsbackend\RequestRelation\Service; | |
| 4 | ||
| 5 | use BO\Zmsentities\RequestRelation as Entity; | |
| 6 | use BO\Zmsentities\Collection\RequestRelationList as Collection; | |
| 7 | ||
| 8 | class RequestRelation extends \BO\Zmsbackend\Base | |
| 9 | { | |
| 10 | public function readEntity($requestId, $providerId, int $resolveReferences = 0): Entity | |
| 11 | { | |
| 12 | $query = new \BO\Zmsbackend\RequestRelation\Repository\RequestRelation(\BO\Zmsbackend\Query\Base::SELECT); | |
| 13 | $query | |
| 14 | ->setResolveLevel(0) | |
| 15 | ->addEntityMapping() | |
| 16 | ->addResolvedReferences($resolveReferences) | |
| 17 | ->addConditionBookable() | |
| 18 | ->addConditionProviderId($providerId) | |
| 19 | ->addConditionRequestId($requestId); | |
| 20 | return $this->fetchOne($query, new Entity()); | |
| 21 | } | |
| 22 | ||
| 23 | public function readListBySource($source, int $resolveReferences = 0) | |
| 24 | { | |
| 25 | $query = new \BO\Zmsbackend\RequestRelation\Repository\RequestRelation(\BO\Zmsbackend\Query\Base::SELECT); | |
| 26 | $query | |
| 27 | ->setResolveLevel(0) | |
| 28 | ->addEntityMapping() | |
| 29 | ->addResolvedReferences($resolveReferences) | |
| 30 | ->addConditionBookable() | |
| 31 | ->addConditionSource($source); | |
| 32 | $statement = $this->fetchStatement($query); | |
| 33 | return $this->readList($statement); | |
| 34 | } | |
| 35 | ||
| 36 | public function readListByRequestId($requestId, $source, int $resolveReferences = 0) | |
| 37 | { | |
| 38 | $query = new \BO\Zmsbackend\RequestRelation\Repository\RequestRelation(\BO\Zmsbackend\Query\Base::SELECT); | |
| 39 | $query | |
| 40 | ->setResolveLevel(0) | |
| 41 | ->addEntityMapping() | |
| 42 | ->addResolvedReferences($resolveReferences) | |
| 43 | ->addConditionRequestId($requestId) | |
| 44 | ->addConditionBookable() | |
| 45 | ->addConditionSource($source); | |
| 46 | $statement = $this->fetchStatement($query); | |
| 47 | return $this->readList($statement); | |
| 48 | } | |
| 49 | ||
| 50 | public function readListByProviderId($providerId, $source, int $resolveReferences = 0) | |
| 51 | { | |
| 52 | $query = new \BO\Zmsbackend\RequestRelation\Repository\RequestRelation(\BO\Zmsbackend\Query\Base::SELECT); | |
| 53 | $query | |
| 54 | ->setResolveLevel(0) | |
| 55 | ->addEntityMapping() | |
| 56 | ->addResolvedReferences($resolveReferences) | |
| 57 | ->addConditionProviderId($providerId) | |
| 58 | ->addConditionBookable() | |
| 59 | ->addConditionSource($source); | |
| 60 | $statement = $this->fetchStatement($query); | |
| 61 | return $this->readList($statement); | |
| 62 | } | |
| 63 | ||
| 64 | public function writeListBySource(\BO\Zmsentities\Source $source) | |
| 65 | { | |
| 66 | if ($source->getRequestRelationList()->count()) { | |
| 67 | foreach ($source->getRequestRelationList() as $entity) { | |
| 68 | $this->writeEntity($entity); | |
| 69 | } | |
| 70 | } elseif ($source->isCompleteAndEditable()) { | |
| 71 | foreach ($source->getProviderList() as $provider) { | |
| 72 | foreach ($source->getRequestList() as $request) { | |
| 73 | $entity = new Entity([ | |
| 74 | 'source' => $source->getSource(), | |
| 75 | 'provider' => $provider, | |
| 76 | 'request' => $request, | |
| 77 | 'slots' => 1, | |
| 78 | 'public' => true, | |
| 79 | 'maxQuantity' => null, | |
| 80 | ]); | |
| 81 | $this->writeEntity($entity); | |
| 82 | } | |
| 83 | } | |
| 84 | } | |
| 85 | ||
| 86 | return $this->readListBySource($source->getSource()); | |
| 87 | } | |
| 88 | ||
| 89 | public function writeEntity(Entity $entity) | |
| 90 | { | |
| 91 | $query = new \BO\Zmsbackend\RequestRelation\Repository\RequestRelation(\BO\Zmsbackend\Query\Base::REPLACE); | |
| 92 | $query->addValues([ | |
| 93 | 'source' => $entity->getSource(), | |
| 94 | 'provider__id' => $entity->getProvider()->getId(), | |
| 95 | 'request__id' => $entity->getRequest()->getId(), | |
| 96 | 'slots' => $entity->getSlotCount(), | |
| 97 | 'public_visibility' => $entity->isPublic() ? 1 : 0, | |
| 98 | 'max_quantity' => $entity->getMaxQuantity() | |
| 99 | ]); | |
| 100 | $this->writeItem($query); | |
| 101 | return $this->readEntity($entity->getRequest()->getId(), $entity->getProvider()->getId()); | |
| 102 | } | |
| 103 | ||
| 104 | public function writeImportList($providerList, $source = 'dldb') | |
| 105 | { | |
| 106 | foreach ($providerList as $provider) { | |
| 107 | // Do not import locations without address | |
| 108 | if ($provider['address']['postal_code']) { | |
| 109 | foreach ($provider['services'] as $reference) { | |
| 110 | $query = new \BO\Zmsbackend\RequestRelation\Repository\RequestRelation(\BO\Zmsbackend\Query\Base::REPLACE); | |
| 111 | $query->addValues([ | |
| 112 | 'source' => $source, | |
| 113 | 'provider__id' => $provider['id'], | |
| 114 | 'request__id' => $reference['service'], | |
| 115 | 'slots' => $reference['appointment']['slots'], | |
| 116 | 'public_visibility' => isset($reference['public']) && !$reference['public'] ? 0 : 1, | |
| 117 | 'max_quantity' => $reference['maxQuantity'] ?? null, | |
| 118 | 'bookable' => ($reference['appointment']['allowed'] ? 1 : 0) | |
| 119 | ]); | |
| 120 | $this->writeItem($query); | |
| 121 | } | |
| 122 | } | |
| 123 | } | |
| 124 | return $this->readListBySource($source); | |
| 125 | } | |
| 126 | ||
| 127 | protected function readList($statement): Collection | |
| 128 | { | |
| 129 | $collection = new Collection(); | |
| 130 | while ($requestRelationData = $statement->fetch(\PDO::FETCH_ASSOC)) { | |
| 131 | $entity = new Entity($requestRelationData); | |
| 132 | $collection->addEntity($entity); | |
| 133 | } | |
| 134 | return $collection; | |
| 135 | } | |
| 136 | ||
| 137 | public function writeDeleteListBySource(string $source): bool | |
| 138 | { | |
| 139 | $query = new \BO\Zmsbackend\RequestRelation\Repository\RequestRelation(\BO\Zmsbackend\Query\Base::DELETE); | |
| 140 | $query->addConditionSource($source); | |
| 141 | return $this->deleteItem($query); | |
| 142 | } | |
| 143 | } |