Completed
Push — 1.4-new-behat ( e12fcf...33b548 )
by Kamil
27:34
created

themeChangesPluginMainTemplateContent()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sylius\Behat\Context\Setup;
15
16
use Behat\Behat\Context\Context;
17
use Doctrine\Common\Persistence\ObjectManager;
18
use Sylius\Behat\Service\SharedStorageInterface;
19
use Sylius\Bundle\ThemeBundle\Configuration\Test\TestThemeConfigurationManagerInterface;
20
use Sylius\Bundle\ThemeBundle\Model\ThemeInterface;
21
use Sylius\Bundle\ThemeBundle\Repository\ThemeRepositoryInterface;
22
use Sylius\Component\Core\Model\ChannelInterface;
23
24
final class ThemeContext implements Context
25
{
26
    /** @var SharedStorageInterface */
27
    private $sharedStorage;
28
29
    /** @var ThemeRepositoryInterface */
30
    private $themeRepository;
31
32
    /** @var ObjectManager */
33
    private $channelManager;
34
35
    /** @var TestThemeConfigurationManagerInterface */
36
    private $testThemeConfigurationManager;
37
38
    public function __construct(
39
        SharedStorageInterface $sharedStorage,
40
        ThemeRepositoryInterface $themeRepository,
41
        ObjectManager $channelManager,
42
        TestThemeConfigurationManagerInterface $testThemeConfigurationManager
43
    ) {
44
        $this->sharedStorage = $sharedStorage;
45
        $this->themeRepository = $themeRepository;
46
        $this->channelManager = $channelManager;
47
        $this->testThemeConfigurationManager = $testThemeConfigurationManager;
48
    }
49
50
    /**
51
     * @Given the store has :themeName theme
52
     */
53
    public function storeHasTheme($themeName)
54
    {
55
        $this->testThemeConfigurationManager->add([
56
            'name' => $themeName,
57
        ]);
58
59
        $this->sharedStorage->set('theme', $this->themeRepository->findOneByName($themeName));
60
    }
61
62
    /**
63
     * @Given channel :channel uses :theme theme
64
     */
65
    public function channelUsesTheme(ChannelInterface $channel, ThemeInterface $theme)
66
    {
67
        $channel->setThemeName($theme->getName());
68
69
        $this->channelManager->persist($channel);
70
        $this->channelManager->flush();
71
72
        $this->sharedStorage->set('channel', $channel);
73
        $this->sharedStorage->set('theme', $theme);
74
    }
75
76
    /**
77
     * @Given channel :channel does not use any theme
78
     */
79
    public function channelDoesNotUseAnyTheme(ChannelInterface $channel)
80
    {
81
        $channel->setThemeName(null);
82
83
        $this->channelManager->flush();
84
85
        $this->sharedStorage->set('channel', $channel);
86
    }
87
88
    /**
89
     * @Given /^(this theme) changes homepage template contents to "([^"]+)"$/
90
     */
91
    public function themeChangesHomepageTemplateContents(ThemeInterface $theme, $contents)
92
    {
93
        $this->changeTemplateContents('/SyliusShopBundle/views/Homepage/index.html.twig', $theme, $contents);
94
    }
95
96
    /**
97
     * @Given /^(this theme) changes plugin main template's content to "([^"]+)"$/
98
     */
99
    public function themeChangesPluginMainTemplateContent(ThemeInterface $theme, string $content): void
100
    {
101
        $this->changeTemplateContents('/SyliusTestPlugin/views/main.html.twig', $theme, $content);
102
    }
103
104
    private function changeTemplateContents(string $templatePath, ThemeInterface $theme, string $contents): void
105
    {
106
        $file = rtrim($theme->getPath(), '/') . $templatePath;
107
        $dir = dirname($file);
108
109
        if (!file_exists($dir)) {
110
            mkdir($dir, 0777, true);
111
        }
112
113
        file_put_contents($file, $contents);
114
    }
115
}
116