1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60  61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80  81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 
<?php

/* --------------------------------------------------------------
  StaticSeoUrlRepository.inc.php 2017-05-24
  Gambio GmbH
  http://www.gambio.de
  Copyright (c) 2017 Gambio GmbH
  Released under the GNU General Public License (Version 2)
  [http://www.gnu.org/licenses/gpl-2.0.html]
  --------------------------------------------------------------
*/

MainFactory::load_class('StaticSeoUrlRepositoryInterface');

/**
 * Class StaticSeoUrlRepository
 *
 * @category   System
 * @package    StaticSeoUrl
 * @subpackage Repositories
 */
class StaticSeoUrlRepository implements StaticSeoUrlRepositoryInterface
{
    /**
     * @var StaticSeoUrlRepositoryWriterInterface
     */
    protected $writer;
    
    /**
     * @var  StaticSeoUrlRepositoryReaderInterface
     */
    protected $reader;
    
    /**
     * @var  StaticSeoUrlRepositoryDeleterInterface
     */
    protected $deleter;
    
    /**
     * @var  StaticSeoUrlContentRepositoryInterface
     */
    protected $staticSeoUrlContentRepository;
    
    
    /**
     * StaticSeoUrlRepository constructor.
     *
     * @param StaticSeoUrlRepositoryWriterInterface  $writer
     * @param StaticSeoUrlRepositoryReaderInterface  $reader
     * @param StaticSeoUrlRepositoryDeleterInterface $deleter
     * @param StaticSeoUrlContentRepositoryInterface $staticSeoUrlContentRepository
     */
    public function __construct(StaticSeoUrlRepositoryWriterInterface $writer,
                                StaticSeoUrlRepositoryReaderInterface $reader,
                                StaticSeoUrlRepositoryDeleterInterface $deleter,
                                StaticSeoUrlContentRepositoryInterface $staticSeoUrlContentRepository)
    {
        $this->writer                        = $writer;
        $this->reader                        = $reader;
        $this->deleter                       = $deleter;
        $this->staticSeoUrlContentRepository = $staticSeoUrlContentRepository;
    }
    
    
    /**
     * Returns a StaticSeoUrlCollection with all existing StaticSeoUrl objects.
     *
     * @return StaticSeoUrlCollection
     */
    public function getAll()
    {
        $staticSeoUrlCollection = $this->reader->getAll();
        
        /** @var StaticSeoUrlInterface $staticSeoUrl */
        foreach($staticSeoUrlCollection->getArray() as $staticSeoUrl)
        {
            $staticSeoUrl->setStaticSeoUrlContentCollection($this->_getStaticSeoUrlContentCollection(new IdType($staticSeoUrl->getId())));
        }
        
        return $staticSeoUrlCollection;
    }
    
    
    /**
     * Returns a StaticSeoUrl instance by the given staticSeoUrl ID.
     *
     * @param IdType $staticSeoUrlId ID of the staticSeoUrl to get.
     *
     * @return StaticSeoUrlInterface
     */
    public function getById(IdType $staticSeoUrlId)
    {
        $staticSeoUrl = $this->reader->getById($staticSeoUrlId);
        $staticSeoUrl->setStaticSeoUrlContentCollection($this->_getStaticSeoUrlContentCollection(new IdType($staticSeoUrl->getId())));
        
        return $staticSeoUrl;
    }
    
    
    /**
     * Stores a StaticSeoUrl to the database.
     *
     * @param StaticSeoUrlInterface $staticSeoUrl StaticSeoUrl to store.
     *
     * @throws InvalidArgumentException If no staticSeoUrl was found by the given ID.
     *
     * @return StaticSeoUrlRepositoryInterface Same instance for method chaining.
     */
    public function store(StaticSeoUrlInterface $staticSeoUrl)
    {
        $staticSeoUrlId = new IdType($this->writer->store($staticSeoUrl));
        $staticSeoUrl->setId($staticSeoUrlId);
        
        foreach($staticSeoUrl->getStaticSeoUrlContentCollection()->getArray() as $staticSeoUrlContent)
        {
            $this->staticSeoUrlContentRepository->store($staticSeoUrlId, $staticSeoUrlContent);
        }
        
        return $this;
    }
    
    
    /**
     * Deletes a StaticSeoUrl by the given staticSeoUrl ID.
     *
     * @param IdType $staticSeoUrlId ID of the staticSeoUrl to delete.
     *
     * @throws InvalidArgumentException If no staticSeoUrl was found by the given ID.
     *
     * @return StaticSeoUrlRepositoryInterface Same instance for method chaining.
     */
    public function deleteStaticSeoUrlById(IdType $staticSeoUrlId)
    {
        $staticSeoUrl = $this->getById($staticSeoUrlId);
        $this->deleter->deleteById($staticSeoUrlId);
        
        foreach($staticSeoUrl->getStaticSeoUrlContentCollection()->getArray() as $staticSeoUrlContent)
        {
            $this->staticSeoUrlContentRepository->deleteStaticSeoUrlContentById(new IdType($staticSeoUrlContent->getId()));
        }
        
        return $this;
    }
    
    
    /**
     * Returns a StaticSeoUrlContentCollection with all existing StaticSeoUrlContent objects by the given staticSeoUrl
     * ID.
     *
     * @param IdType $staticSeoUrlId ID of the staticSeoUrl to fetch the staticSeoUrlContent collection from.
     *
     * @return StaticSeoUrlContentCollection
     */
    protected function _getStaticSeoUrlContentCollection(IdType $staticSeoUrlId)
    {
        return $this->staticSeoUrlContentRepository->getByStaticSeoUrlId($staticSeoUrlId);
    }
}