| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * apparat-dev | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @category    Apparat | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @package     Apparat\Dev | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @subpackage  Apparat\Dev\Infrastructure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @author      Joschi Kuphal <[email protected]> / @jkphl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * @license     http://opensource.org/licenses/MIT The MIT License (MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | /*********************************************************************************** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  *  The MIT License (MIT) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  *  Permission is hereby granted, free of charge, to any person obtaining a copy of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  *  this software and associated documentation files (the "Software"), to deal in | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  *  the Software without restriction, including without limitation the rights to | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  *  the Software, and to permit persons to whom the Software is furnished to do so, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  *  subject to the following conditions: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  *  The above copyright notice and this permission notice shall be included in all | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |  *  copies or substantial portions of the Software. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  ***********************************************************************************/ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | namespace Apparat\Dev\Infrastructure\Factory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | use Apparat\Dev\Ports\Repository; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | use Apparat\Dev\Ports\RuntimeException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | use Apparat\Kernel\Ports\Kernel; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | use Apparat\Object\Domain\Model\Uri\RepositoryLocator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | use Apparat\Object\Domain\Model\Uri\RepositoryLocatorInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | use Apparat\Object\Infrastructure\Repository\FileAdapterStrategy; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  * Shallow object factory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  * @package Apparat\Dev | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  * @subpackage Apparat\Dev\Infrastructure | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | class ShallowObjectFactory extends AbstractObjectFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * Container factory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @var ContainerFactory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     protected $containerFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * Constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * @param ContainerFactory $containerFactory Container factory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 3 |  |     public function __construct(ContainerFactory $containerFactory) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 3 |  |         $this->containerFactory = $containerFactory; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 | 3 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * Create a repository object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @param \DateTimeInterface $creationDate Creation date | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      * @param int $objectId Object ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |      * @param array $config Object configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |      * @throws RuntimeException If the resource container cannot be created | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |      * @throws RuntimeException If the shallow resource cannot be created | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 3 |  |     public function create(\DateTimeInterface $creationDate, $objectId, array $config) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         // Build the container path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 3 |  |         $hidden = ($config[Repository::HIDDEN] > 0) ? (rand(0, 100) < ($config[Repository::HIDDEN] * 100)) : false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $containerPath = '/'. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 3 |  |             $this->containerFactory->create( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 $creationDate, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 $hidden, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 $objectId, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 3 |  |                 $config[Repository::TYPE] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 3 |  |             ).'/'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 3 |  |         $this->createObjectRevisions($objectId, $config, $containerPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * Create the object revisions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @param int $objectId Object ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |      * @param array $config Object configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |      * @param string $containerPath Repository relative container path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 3 |  |     protected function createObjectRevisions($objectId, array $config, $containerPath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |         // Run through all object paths and create (empty) files | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         /** @var FileAdapterStrategy $adapterStrategy */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 | 3 |  |         $adapterStrategy = $this->repository->getAdapterStrategy(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 | 3 |  |         foreach ($this->revisionPaths($objectId, $config, $containerPath) as $revisionPathIndex => $revisionPath) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 | 3 |  |             $this->createObjectResource($revisionPathIndex, $revisionPath, $adapterStrategy); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |      * Generate the list of repository relative revision paths | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @param int $objectId Object ID | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @param array $config Object configuration | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      * @param string $containerPath Repository relative container path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * @return array Repository relative revision paths | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 3 |  |     protected function revisionPaths($objectId, array $config, $containerPath) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         // Determine the amount of archive revisions to create | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 3 |  |         $revisions = ($config[Repository::REVISIONS] < 0) ? | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 3 |  |             rand(1, abs($config[Repository::REVISIONS]) + 1) : (1 + $config[Repository::REVISIONS]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |         // Determine the draft status | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 3 |  |         $draft = (($config[Repository::DRAFTS] > 0) ? (rand(0, 100) < $config[Repository::DRAFTS] * 100) : 0); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |         // Build the revision path list | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 | 3 |  |         $revisionPaths = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 | 3 |  |         for ($revision = 1; $revision < $revisions - $draft; ++$revision) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 2 |  |             $revisionPaths[] = $containerPath.$objectId.'-'.$revision.'.'.getenv('OBJECT_RESOURCE_EXTENSION'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 3 |  |         if ($revision <= $revisions - $draft) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 | 3 |  |             $revisionPaths[] = $containerPath.$objectId.'.'.getenv('OBJECT_RESOURCE_EXTENSION'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 | 3 |  |         if ($draft) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 | 1 |  |             $revisionPaths[] = $containerPath.($draft ? '.' : '').$objectId.'-'.$revisions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 | 1 |  |                 '.'.getenv('OBJECT_RESOURCE_EXTENSION'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 3 |  |         return $revisionPaths; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      * Create a single object resource | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @param int $revisionPathIndex Revision path index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param string $revisionPath Revision repository path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @param FileAdapterStrategy $adapterStrategy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * @throws RuntimeException If the shallow resource cannot be created | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 | 3 |  |     protected function createObjectResource($revisionPathIndex, $revisionPath, FileAdapterStrategy $adapterStrategy) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |         /** @var RepositoryLocatorInterface $revRepositoryLocator */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 | 3 |  |         $revRepositoryLocator = Kernel::create(RepositoryLocator::class, [$this->repository, $revisionPath]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 | 3 |  |         $absRevisionPath = $adapterStrategy->getAbsoluteResourcePath($revRepositoryLocator); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         // For the first revision: Create the container directory | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 | 3 |  |         if (!$revisionPathIndex) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 | 3 |  |             $this->createObjectResourceContainer(dirname($absRevisionPath)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         // If the shallow resource cannot be created | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 | 2 |  |         if (!touch($absRevisionPath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 | 1 |  |             throw new RuntimeException( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 | 1 |  |                 sprintf('Cannot create shallow resource "%s"', $absRevisionPath), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 | 1 |  |                 RuntimeException::CANNOT_CREATE_SHALLOW_RESOURCE | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |             ); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 | 1 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * Create the object resource container | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |      * @param string $absContainerPath Absolute object resource container path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 178 |  |  |      * @throws RuntimeException If the resource container cannot be created | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 179 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 180 | 3 |  |     protected function createObjectResourceContainer($absContainerPath) | 
            
                                                        
            
                                    
            
            
                | 181 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 182 |  |  |         // If the resource container cannot be created | 
            
                                                        
            
                                    
            
            
                | 183 | 3 |  |         if (!is_dir($absContainerPath) && !mkdir($absContainerPath, 0777, true)) { | 
            
                                                        
            
                                    
            
            
                | 184 | 1 |  |             throw new RuntimeException( | 
            
                                                        
            
                                    
            
            
                | 185 | 1 |  |                 sprintf('Cannot create container directory "%s"', $absContainerPath), | 
            
                                                        
            
                                    
            
            
                | 186 | 1 |  |                 RuntimeException::CANNOT_CREATE_CONTAINER_DIRECTORY | 
            
                                                        
            
                                    
            
            
                | 187 |  |  |             ); | 
            
                                                        
            
                                    
            
            
                | 188 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 189 | 2 |  |     } | 
            
                                                        
            
                                    
            
            
                | 190 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 191 |  |  |  |