| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace keeko\core\domain\base; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use keeko\core\model\Module; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use keeko\core\model\ModuleQuery; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use keeko\framework\service\ServiceContainer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use keeko\framework\domain\payload\PayloadInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use phootwork\collection\Map; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use keeko\framework\domain\payload\Found; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use keeko\framework\domain\payload\NotFound; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use Tobscure\JsonApi\Parameters; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | use keeko\framework\utils\NameUtils; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use keeko\framework\domain\payload\Created; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use keeko\framework\domain\payload\Updated; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | use keeko\framework\domain\payload\NotUpdated; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | use keeko\framework\domain\payload\NotValid; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use keeko\framework\domain\payload\Deleted; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use keeko\framework\domain\payload\NotDeleted; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | trait ModuleDomainTrait { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 	 * Creates a new Module with the provided data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * @param mixed $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * @return PayloadInterface | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 29 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 30 |  |  | 	public function create($data) { | 
            
                                                                        
                            
            
                                    
            
            
                | 31 |  |  | 		// hydrate | 
            
                                                                        
                            
            
                                    
            
            
                | 32 |  |  | 		$serializer = Module::getSerializer(); | 
            
                                                                        
                            
            
                                    
            
            
                | 33 |  |  | 		$module = $serializer->hydrate(new Module(), $data); | 
            
                                                                        
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 35 |  |  | 		// validate | 
            
                                                                        
                            
            
                                    
            
            
                | 36 |  |  | 		if (!$module->validate()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 37 |  |  | 			return new NotValid([ | 
            
                                                                        
                            
            
                                    
            
            
                | 38 |  |  | 				'errors' => $module->getValidationFailures() | 
            
                                                                        
                            
            
                                    
            
            
                | 39 |  |  | 			]); | 
            
                                                                        
                            
            
                                    
            
            
                | 40 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 42 |  |  | 		$module->save(); | 
            
                                                                        
                            
            
                                    
            
            
                | 43 |  |  | 		return new Created(['model' => $module]); | 
            
                                                                        
                            
            
                                    
            
            
                | 44 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 	 * Deletes a Module with the given id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @param mixed $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 * @return PayloadInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	public function delete($id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		// find | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		$module = $this->get($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		if ($module === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			return new NotFound(['message' => 'Module not found.']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		// delete | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		$module->delete(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 		if ($module->isDeleted()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			return new Deleted(['model' => $module]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		return new NotDeleted(['message' => 'Could not delete Module']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 	 * Returns a paginated result | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 	 * @param Parameters $params | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 	 * @return PayloadInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	public function paginate(Parameters $params) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 		$sysPrefs = $this->getServiceContainer()->getPreferenceLoader()->getSystemPreferences(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		$defaultSize = $sysPrefs->getPaginationSize(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 		$page = $params->getPage('number'); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 		$size = $params->getPage('size', $defaultSize); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		$query = ModuleQuery::create(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		// sorting | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 		$sort = $params->getSort(Module::getSerializer()->getSortFields()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 		foreach ($sort as $field => $order) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 			$method = 'orderBy' . NameUtils::toStudlyCase($field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 			$query->$method($order); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 		// filtering | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 		$filter = $params->getFilter(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 		if (!empty($filter)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 			$this->applyFilter($query, $filter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 		// paginate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 		$module = $query->paginate($page, $size); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		// run response | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		return new Found(['model' => $module]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	 * Returns one Module with the given id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 	 * @param mixed $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 	 * @return PayloadInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 	public function read($id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		// read | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 		$module = $this->get($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		// check existence | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 		if ($module === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 			return new NotFound(['message' => 'Module not found.']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 		return new Found(['model' => $module]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 	 * Updates a Module with the given idand the provided data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 	 * @param mixed $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 	 * @param mixed $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	 * @return PayloadInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	public function update($id, $data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 		// find | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 		$module = $this->get($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 		if ($module === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 			return new NotFound(['message' => 'Module not found.']); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		// hydrate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		$serializer = Module::getSerializer(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 		$module = $serializer->hydrate($module, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 		// validate | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		if (!$module->validate()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 			return new NotValid([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 				'errors' => $module->getValidationFailures() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 			]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 		$rows = $module->save(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  | 		$payload = ['model' => $module]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		if ($rows === 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  | 			return new NotUpdated($payload); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  | 		return new Updated($payload); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  | 	 * Implement this functionality at keeko\core\domain\ModuleDomain | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  | 	 * @param ModuleQuery $query | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  | 	 * @param mixed $filter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  | 	abstract protected function applyFilter(ModuleQuery $query, $filter); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  | 	 * Returns one Module with the given id from cache | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  | 	 * @param mixed $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  | 	 * @return Module|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  | 	protected function get($id) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  | 		if ($this->pool === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  | 			$this->pool = new Map(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  | 		} else if ($this->pool->has($id)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  | 			return $this->pool->get($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 179 |  |  | 		$module = ModuleQuery::create()->findOneById($id); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 180 |  |  | 		$this->pool->set($id, $module); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 181 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 182 |  |  | 		return $module; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  | 	 * Returns the service container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  | 	 *  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  | 	 * @return ServiceContainer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  | 	abstract protected function getServiceContainer(); | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 191 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 192 |  |  |  | 
            
                        
This check looks for access to methods that are not accessible from the current context.
If you need to make a method accessible to another context you can raise its visibility level in the defining class.