GeneratePictureUriAliasOnModelAfterSaveListener.php 3.14 KB
Newer Older
1
<?php
Tino Goratsch's avatar
Tino Goratsch committed
2

3
/**
4
 * Copyright (c) by the ACP3 Developers.
Tino Goratsch's avatar
Tino Goratsch committed
5
 * See the LICENSE file at the top-level module directory for licensing details.
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
 */

namespace ACP3\Modules\ACP3\Gallery\Event\Listener;

use ACP3\Core\Model\Event\ModelSaveEvent;
use ACP3\Modules\ACP3\Gallery;
use ACP3\Modules\ACP3\Seo\Core\Router\Aliases;
use ACP3\Modules\ACP3\Seo\Helper\MetaStatements;
use ACP3\Modules\ACP3\Seo\Helper\UriAliasManager;

class GeneratePictureUriAliasOnModelAfterSaveListener
{
    /**
     * @var Gallery\Model\Repository\PictureRepository
     */
    private $pictureRepository;
    /**
     * @var Aliases
     */
    private $aliases;
    /**
     * @var MetaStatements
     */
    private $metaStatements;
    /**
     * @var UriAliasManager
     */
    private $uriAliasManager;

    /**
     * UpdateUriAliasesOnModelAfterSaveListener constructor.
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
     * @param Gallery\Model\Repository\PictureRepository $pictureRepository
     */
    public function __construct(Gallery\Model\Repository\PictureRepository $pictureRepository)
    {
        $this->pictureRepository = $pictureRepository;
    }

    /**
     * @param \ACP3\Modules\ACP3\Seo\Core\Router\Aliases $aliases
     */
    public function setAliases(Aliases $aliases)
    {
        $this->aliases = $aliases;
    }

    /**
     * @param \ACP3\Modules\ACP3\Seo\Helper\MetaStatements $metaStatements
     */
    public function setMetaStatements(MetaStatements $metaStatements)
    {
        $this->metaStatements = $metaStatements;
    }

    /**
     * @param \ACP3\Modules\ACP3\Seo\Helper\UriAliasManager $uriAliasManager
     */
    public function setUriAliasManager(UriAliasManager $uriAliasManager)
    {
        $this->uriAliasManager = $uriAliasManager;
    }

    /**
     * @param ModelSaveEvent $event
     */
    public function generatePictureAlias(ModelSaveEvent $event)
    {
        if (!$event->isIsNewEntry()) {
            return;
        }

        if ($this->aliases && $this->metaStatements && $this->uriAliasManager) {
            $pictureId = $event->getEntryId();

            $galleryId = $this->pictureRepository->getGalleryIdFromPictureId($pictureId);
Tino Goratsch's avatar
Tino Goratsch committed
82
            $alias = $this->aliases->getUriAlias(\sprintf(Gallery\Helpers::URL_KEY_PATTERN_GALLERY, $galleryId), true);
83 84 85 86
            if (!empty($alias)) {
                $alias .= '/img-' . $pictureId;
            }
            $seoKeywords = $this->metaStatements->getKeywords(
Tino Goratsch's avatar
Tino Goratsch committed
87
                \sprintf(Gallery\Helpers::URL_KEY_PATTERN_GALLERY, $galleryId)
88 89
            );
            $seoDescription = $this->metaStatements->getDescription(
Tino Goratsch's avatar
Tino Goratsch committed
90
                \sprintf(Gallery\Helpers::URL_KEY_PATTERN_GALLERY, $galleryId)
91
            );
92
            $seoRobots = $this->metaStatements->getRobotsSetting(
Tino Goratsch's avatar
Tino Goratsch committed
93
                \sprintf(Gallery\Helpers::URL_KEY_PATTERN_GALLERY, $galleryId)
94
            );
95

96 97
            $robotsSetting = array_flip($this->metaStatements->getRobotsMap());

98
            $this->uriAliasManager->insertUriAlias(
Tino Goratsch's avatar
Tino Goratsch committed
99
                \sprintf(Gallery\Helpers::URL_KEY_PATTERN_PICTURE, $pictureId),
100 101
                $alias,
                $seoKeywords,
102
                $seoDescription,
103
                $robotsSetting[$seoRobots] ?? 0
104 105 106 107
            );
        }
    }
}