Completed
Push — master ( db30e2...3405a3 )
by Paweł
08:38
created

TenantAwareThemeInstaller::install()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Superdesk Web Publisher Core Bundle.
7
 *
8
 * Copyright 2017 Sourcefabric z.ú. and contributors.
9
 *
10
 * For the full copyright and license information, please see the
11
 * AUTHORS and LICENSE files distributed with this source code.
12
 *
13
 * @copyright 2017 Sourcefabric z.ú
14
 * @license http://www.superdesk.org/license
15
 */
16
17
namespace SWP\Bundle\CoreBundle\Theme\Installer;
18
19
use SWP\Bundle\CoreBundle\Twig\Cache\TenantAwareCacheInterface;
20
use SWP\Component\MultiTenancy\Context\TenantContextInterface;
21
use Sylius\Bundle\ThemeBundle\Asset\Installer\AssetsInstallerInterface;
22
use Sylius\Bundle\ThemeBundle\Loader\ThemeLoaderInterface;
23
use SWP\Bundle\CoreBundle\Theme\Model\ThemeInterface;
24
use Symfony\Component\Filesystem\Filesystem;
25
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
26
27
/**
28
 * Class TenantAwareThemeUploader.
29
 */
30
final class TenantAwareThemeInstaller implements ThemeInstallerInterface
31
{
32
    /**
33
     * @var TenantContextInterface
34
     */
35
    private $tenantContext;
36
37
    /**
38
     * @var ThemeLoaderInterface
39
     */
40
    private $themeLoader;
41
42
    /**
43
     * @var \Twig_Environment
44
     */
45
    private $twig;
46
47
    /**
48
     * @var string
49
     */
50
    private $baseDir;
51
52
    /**
53
     * @var AssetsInstallerInterface
54
     */
55
    private $assetsInstaller;
56
57
    /**
58
     * @var string
59
     */
60
    private $assetsDir;
61
62
    /**
63
     * TenantAwareThemeInstaller constructor.
64
     *
65
     * @param TenantContextInterface   $tenantContext
66
     * @param ThemeLoaderInterface     $themeLoader
67
     * @param \Twig_Environment        $twig
68
     * @param string                   $baseDir
69
     * @param AssetsInstallerInterface $assetsInstaller
70
     * @param string                   $assetsDir
71
     */
72
    public function __construct(
73
        TenantContextInterface $tenantContext,
74
        ThemeLoaderInterface $themeLoader,
75
        \Twig_Environment $twig,
76
        string $baseDir,
77
        AssetsInstallerInterface $assetsInstaller,
78
        string $assetsDir
79
    ) {
80
        $this->tenantContext = $tenantContext;
81
        $this->themeLoader = $themeLoader;
82
        $this->twig = $twig;
83
        $this->baseDir = $baseDir;
84
        $this->assetsInstaller = $assetsInstaller;
85
        $this->assetsDir = $assetsDir;
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91
    public function install($sourceDir = null, $themeDir = null): void
92
    {
93
        $filesystem = new Filesystem();
94
        $filesystem->mirror($sourceDir, $themeDir, null, ['override' => true, 'delete' => true]);
95
96
        $cache = $this->twig->getCache();
97
        if ($cache instanceof TenantAwareCacheInterface) {
98
            $filesystem->remove($cache->generateCacheDir());
99
        }
100
101
        $this->assetsInstaller->installAssets($this->assetsDir, AssetsInstallerInterface::HARD_COPY);
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function getThemeFromOrganizationThemes(string $themeName): ThemeInterface
108
    {
109
        $themes = \array_filter(
110
            $this->themeLoader->load(),
111
            function ($element) use (&$themeName) {
112
                return $element->getName() === $themeName;
113
            }
114
        );
115
116
        if (0 === count($themes)) {
117
            throw new NotFoundHttpException(
118
                \sprintf('Theme with name "%s" was not found in organization themes.', $themeName)
119
            );
120
        }
121
122
        return \reset($themes);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function getThemesPath()
129
    {
130
        if (null === $tenant = $this->tenantContext->getTenant()) {
131
            throw new \Exception('Tenant was not found in context!');
132
        }
133
134
        return $this->baseDir.DIRECTORY_SEPARATOR.$tenant->getCode();
135
    }
136
}
137