Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
save.php
1<?php
2
4
13
18class Save
19 extends Base
20 implements ISave, ISaveParents
21{
23 protected $locationRepositories = [];
24
26 public function setLocationRepositories(array $locationRepositories): Base
27 {
28 $idx = 0;
29
30 foreach($locationRepositories as $repository)
31 {
32 if($repository instanceof ISave)
33 {
34 $key = (string)$this->getRepoPriority($repository) . (string)($idx++);
35 $this->locationRepositories[$key] = $repository;
36 }
37 }
38
39 ksort($this->locationRepositories);
40 return $this;
41 }
42
47 protected function getRepoPriority(IRepository $repository)
48 {
49 if($repository instanceof IDatabase)
50 {
51 $result = self::REPO_PRIORITY_A;
52 }
53 elseif($repository instanceof ICache)
54 {
55 $result = self::REPO_PRIORITY_B;
56 }
57 else
58 {
59 $result = self::REPO_PRIORITY_C;
60 }
61
62 return $result;
63 }
64
72 public function save(Location $location): Result
73 {
74 if($location->getId() <= 0)
75 {
76 return (new NewItem($this->locationRepositories))
77 ->save($location);
78 }
79
80 $result = new Result();
81
82 foreach($this->locationRepositories as $repository)
83 {
84 $res = $repository->save($location);
85
86 if(!$res->isSuccess())
87 {
88 $result->addErrors($res->getErrors());
89 }
90 }
91
92 if($parents = $location->getParents())
93 {
94 $res = $this->saveParents($parents);
95
96 if (!$res->isSuccess())
97 {
98 $result->addErrors($res->getErrors());
99 }
100 }
101
102 return $result;
103 }
104
105 public function saveParents(Location\Parents $parents): Result
106 {
107 $result = new Result();
108
109 foreach($this->locationRepositories as $repository)
110 {
111 if($repository instanceof ISaveParents)
112 {
113 $res = $repository->saveParents($parents);
114
115 if (!$res->isSuccess())
116 {
117 $result->addErrors($res->getErrors());
118 }
119 }
120 }
121
122 return $result;
123 }
124}
getRepoPriority(IRepository $repository)
Definition save.php:47
saveParents(Location\Parents $parents)
Definition save.php:105
setLocationRepositories(array $locationRepositories)
Definition save.php:26