1 <?php
2
3 /* --------------------------------------------------------------
4 CategoryRepository.php 2016-01-29
5 Gambio GmbH
6 http://www.gambio.de
7 Copyright (c) 2016 Gambio GmbH
8 Released under the GNU General Public License (Version 2)
9 [http://www.gnu.org/licenses/gpl-2.0.html]
10 --------------------------------------------------------------
11 */
12
13 /**
14 * Class CategoryRepository
15 *
16 * This class handles the database operations that concern the category records of the
17 * database. It provides a layer for more complicated methods that use the writer, reader and deleter.
18 *
19 * @category System
20 * @package Category
21 * @subpackage Repositories
22 */
23 class CategoryRepository implements CategoryRepositoryInterface
24 {
25 /**
26 * Category repository reader.
27 * @var CategoryRepositoryReaderInterface
28 */
29 protected $reader;
30
31 /**
32 * Category repository writer.
33 * @var CategoryRepositoryWriterInterface
34 */
35 protected $writer;
36
37 /**
38 * Category repository deleter.
39 * @var CategoryRepositoryDeleterInterface
40 */
41 protected $deleter;
42
43 /**
44 * Category settings repository.
45 * @var CategorySettingsRepositoryInterface
46 */
47 protected $settingsRepo;
48
49 /**
50 * Addon value service.
51 * @var AddonValueServiceInterface
52 */
53 protected $addonValueService;
54
55
56 /**
57 * Customer Status Provider
58 * @var CustomerStatusProviderInterface
59 */
60 protected $customerStatusProvider;
61
62
63 /**
64 * Initialize the category repository.
65 *
66 * @param CategoryRepositoryReaderInterface $reader Reader instance to fetch category data from
67 * the storage.
68 * @param CategoryRepositoryWriterInterface $writer Writer instance to store or add category data
69 * in the storage.
70 * @param CategoryRepositoryDeleterInterface $deleter Deleter instance to remove category data from
71 * the storage.
72 * @param CategorySettingsRepositoryInterface $settingsRepo Category setting repository to
73 * save/add/remove
74 * category settings in the storage.
75 * @param AddonValueServiceInterface $addonValueService Addon value service instance to handle the
76 * category addon values.
77 * @param CustomerStatusProviderInterface $customerStatusProvider Customer status provider to handle group
78 * permissions
79 */
80 public function __construct(CategoryRepositoryReaderInterface $reader,
81 CategoryRepositoryWriterInterface $writer,
82 CategoryRepositoryDeleterInterface $deleter,
83 CategorySettingsRepositoryInterface $settingsRepo,
84 AddonValueServiceInterface $addonValueService,
85 CustomerStatusProviderInterface $customerStatusProvider)
86 {
87 $this->reader = $reader;
88 $this->writer = $writer;
89 $this->deleter = $deleter;
90 $this->settingsRepo = $settingsRepo;
91 $this->addonValueService = $addonValueService;
92 $this->customerStatusProvider = $customerStatusProvider;
93 }
94
95
96 /**
97 * Adds a category.
98 *
99 * @param CategoryInterface $category Category.
100 *
101 * @return int Stored ID of the passed category.
102 */
103 public function add(CategoryInterface $category)
104 {
105 $catId = $this->writer->insert($category);
106 $categoryId = MainFactory::create('IdType', $catId);
107
108 $storedCategory = $this->reader->getById($categoryId);
109
110 $storedCategory->addAddonValues($category->getAddonValues());
111 $this->addonValueService->storeAddonValues($storedCategory);
112
113 $storedCategory->setSettings($category->getSettings());
114 $this->settingsRepo->store($categoryId, $storedCategory->getSettings());
115
116 return $catId;
117 }
118
119
120 /**
121 * Stores a category.
122 *
123 * @param StoredCategoryInterface $category Category.
124 *
125 * @return CategoryRepository Same instance for chained method calls.
126 */
127 public function store(StoredCategoryInterface $category)
128 {
129 $category->setLastModifiedDateTime(new DateTime());
130
131 $this->writer->update($category);
132 $categoryId = MainFactory::create('IdType', $category->getCategoryId());
133
134 $this->settingsRepo->store($categoryId, $category->getSettings());
135 $this->addonValueService->storeAddonValues($category);
136
137 return $this;
138 }
139
140
141 /**
142 * Gets a category by the given ID.
143 *
144 * @param IdType $categoryId Category ID.
145 *
146 * @return StoredCategoryInterface
147 */
148 public function getCategoryById(IdType $categoryId)
149 {
150 $category = $this->reader->getById($categoryId);
151 $category->setSettings($this->settingsRepo->getCategorySettingsById($categoryId));
152 $this->addonValueService->loadAddonValues($category);
153
154 return $category;
155 }
156
157
158 /**
159 * Returns all Categories with the provided parent ID.
160 *
161 * @param IdType $parentId
162 *
163 * @return IdCollection
164 */
165 public function getCategoryIdsByParentId(IdType $parentId)
166 {
167 return $this->reader->getByParentId($parentId);
168 }
169
170
171 /**
172 * Deletes a category by the given ID.
173 *
174 * @param IdType $categoryId Category ID.
175 *
176 * @return CategoryRepository Same instance for chained method calls.
177 */
178 public function deleteCategoryById(IdType $categoryId)
179 {
180 $category = $this->reader->getById($categoryId);
181 $this->addonValueService->deleteAddonValues($category);
182
183 $this->deleter->deleteById($categoryId);
184
185 return $this;
186 }
187 }