Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
75 / 75 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
Notification | |
100.00% |
75 / 75 |
|
100.00% |
6 / 6 |
18 | |
100.00% |
1 / 1 |
readEntity | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
3 | |||
readList | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
readResolvedReferences | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
4 | |||
writeInQueue | |
100.00% |
25 / 25 |
|
100.00% |
1 / 1 |
4 | |||
writeInCalculationTable | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
2 | |||
deleteEntity | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace BO\Zmsdb; |
4 | |
5 | use BO\Zmsentities\Notification as Entity; |
6 | use BO\Zmsentities\Collection\NotificationList as Collection; |
7 | |
8 | class Notification extends Base |
9 | { |
10 | /** |
11 | * Fetch status from db |
12 | * |
13 | * @return \BO\Zmsentities\Mail |
14 | */ |
15 | public function readEntity($itemId, $resolveReferences = 1) |
16 | { |
17 | $query = new Query\Notification(Query\Base::SELECT); |
18 | $query |
19 | ->addEntityMapping() |
20 | ->addResolvedReferences($resolveReferences) |
21 | ->addConditionItemId($itemId); |
22 | $notification = $this->fetchOne($query, new Entity()); |
23 | if ($notification && $notification->hasId()) { |
24 | $notification = $this->readResolvedReferences($notification, $resolveReferences); |
25 | } |
26 | return $notification; |
27 | } |
28 | |
29 | public function readList($resolveReferences = 1) |
30 | { |
31 | $notificationList = new Collection(); |
32 | $query = new Query\Notification(Query\Base::SELECT); |
33 | $query |
34 | ->addEntityMapping() |
35 | ->addResolvedReferences($resolveReferences); |
36 | $result = $this->fetchList($query, new Entity()); |
37 | if (count($result)) { |
38 | foreach ($result as $item) { |
39 | $entity = new Entity($item); |
40 | $entity = $this->readResolvedReferences($entity, $resolveReferences); |
41 | if ($entity instanceof Entity) { |
42 | $notificationList->addEntity($entity); |
43 | } |
44 | } |
45 | } |
46 | return $notificationList; |
47 | } |
48 | |
49 | public function readResolvedReferences(\BO\Zmsentities\Schema\Entity $notification, $resolveReferences) |
50 | { |
51 | if (1 <= $resolveReferences) { |
52 | $processQuery = new \BO\Zmsdb\Process(); |
53 | $process = $processQuery |
54 | ->readEntity( |
55 | $notification->process['id'], |
56 | new Helper\NoAuth(), |
57 | $resolveReferences - 1 |
58 | ); |
59 | // only overwrite process with resolved version if not dereferenced |
60 | if ($process && $notification->getScopeId() == $process->getScopeId()) { |
61 | $notification->process = $process; |
62 | } |
63 | $notification->department = (new \BO\Zmsdb\Department()) |
64 | ->readEntity($notification->department['id'], $resolveReferences - 1); |
65 | } |
66 | return $notification; |
67 | } |
68 | |
69 | |
70 | public function writeInQueue(Entity $notification, \DateTimeInterface $dateTime, $count = true) |
71 | { |
72 | $queueId = null; |
73 | $process = new \BO\Zmsentities\Process($notification->process); |
74 | $client = $process->getFirstClient(); |
75 | if (! $client->hasTelephone()) { |
76 | throw new Exception\Notification\ClientWithoutTelephone(); |
77 | } |
78 | $notification->hasProperties('message', 'process'); |
79 | $telephone = preg_replace('/\s+/', '', $client->telephone); |
80 | $department = (new Department())->readByScopeId($process->getScopeId(), 0); |
81 | $query = new Query\Notification(Query\Base::INSERT); |
82 | $query->addValues(array( |
83 | 'processID' => $notification->process['id'], |
84 | 'scopeID' => $notification->process['scope']['id'], |
85 | 'departmentID' => $department->toProperty()->id->get(), |
86 | 'createIP' => $notification->createIP, |
87 | 'createTimestamp' => time(), |
88 | 'message' => $notification->message, |
89 | 'clientFamilyName' => $client->familyName, |
90 | 'clientTelephone' => $telephone, |
91 | )); |
92 | $success = $this->writeItem($query); |
93 | $queueId = $this->getWriter()->lastInsertId(); |
94 | if ($count && $success) { |
95 | $client->notificationsSendCount += 1; |
96 | (new Process())->updateEntity($process, $dateTime); |
97 | } |
98 | return $this->readEntity($queueId); |
99 | } |
100 | |
101 | public function writeInCalculationTable(\BO\Zmsentities\Schema\Entity $notification) |
102 | { |
103 | $amount = ceil((strlen(trim($notification->message))) / 160); |
104 | $scopeId = $notification->getScopeId(); |
105 | if (!$scopeId) { |
106 | return false; |
107 | } |
108 | $client = $notification->getClient(); |
109 | $query = Query\Notification::QUERY_WRITE_IN_CALCULATION; |
110 | return $this->perform($query, array( |
111 | $scopeId, |
112 | $client->telephone, |
113 | $notification->getCreateDateTime()->format('Y-m-d'), |
114 | $amount |
115 | )); |
116 | } |
117 | |
118 | public function deleteEntity($itemId) |
119 | { |
120 | $query = new Query\Notification(Query\Base::DELETE); |
121 | $query->addConditionItemId($itemId); |
122 | return $this->deleteItem($query); |
123 | } |
124 | } |