Completed
Push — master ( 89bfcf...4ab803 )
by Vladimir
02:41
created

ThemeManager::shouldBeTracked()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
3
/**
4
 * @copyright 2017 Vladimir Jimenez
5
 * @license   https://github.com/allejo/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Manager;
9
10
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
11
use Symfony\Component\Yaml\Yaml;
12
13
class ThemeManager extends AssetManager
14
{
15
    const THEME_FOLDER = '_themes';
16
    const THEME_DEFINITION_FILE = 'stakx-theme.yml';
17
18
    private $themeFolderRelative;
19
    private $themeFolder;
20
    private $themeFile;
21
    private $themeData;
22
    private $themeName;
23
24
    public function __construct($themeName)
25
    {
26
        parent::__construct();
27
28
        $this->themeFolderRelative = $this->fs->appendPath(self::THEME_FOLDER, $themeName);
29
        $this->themeFolder = $this->fs->absolutePath(self::THEME_FOLDER, $themeName);
30
        $this->themeName = $themeName;
31
        $this->themeFile = $this->fs->appendPath($this->themeFolder, self::THEME_DEFINITION_FILE);
32
        $this->themeData = array(
33
            'exclude' => array(),
34
            'include' => array(),
35
        );
36
37
        if (!$this->fs->exists($this->themeFolder))
38
        {
39
            throw new FileNotFoundException("The '${themeName}' theme folder could not be found.'");
40
        }
41
42
        if ($this->fs->exists($this->themeFile))
43
        {
44
            $themeData = Yaml::parse(file_get_contents($this->themeFile));
45
46
            $this->themeData = array_merge_recursive($this->themeData, $themeData);
47
        }
48
49
        foreach ($this->themeData['include'] as &$include)
50
        {
51
            $include = $this->fs->appendPath($this->themeFolder, $include);
52
        }
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 View Code Duplication
    public function refreshItem($filePath)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
59
    {
60
        $relativeFilePath = str_replace($this->themeFolderRelative . '/', '', $filePath);
61
62
        return $this->handleTrackableItem(
63
            $relativeFilePath,
64
            $this->trackedItemsOptions[$filePath]
65
        );
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function shouldBeTracked($filePath)
72
    {
73
        $isThemeAsset = (substr($filePath, 0, strlen($this->themeFolderRelative)) === $this->themeFolderRelative);
74
75
        return $isThemeAsset && parent::shouldBeTracked($filePath);
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 View Code Duplication
    public function createNewItem($filePath)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
82
    {
83
        $relativeFilePath = str_replace($this->themeFolderRelative . '/', '', $filePath);
84
85
        return $this->handleTrackableItem($relativeFilePath, array(
86
            'prefix' => $this->themeFolderRelative,
87
        ));
88
    }
89
90
    public function copyFiles()
91
    {
92
        $this->output->notice('Copying theme files...');
93
94
        $this->scanTrackableItems(
95
            $this->themeFolder,
96
            array(
97
                'prefix' => $this->themeFolderRelative,
98
            ),
99
            array_merge(
100
                $this->includes,
101
                $this->themeData['include']
102
            ),
103
            array_merge(
104
                $this->excludes,
105
                $this->themeData['exclude']
106
            )
107
        );
108
    }
109
}
110