Completed
Push — master ( 7b9015...c5b311 )
by Vladimir
26s queued 11s
created

AssetManager::getExplicitAsset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
ccs 0
cts 4
cp 0
crap 6
1
<?php
2
3
/**
4
 * @copyright 2018 Vladimir Jimenez
5
 * @license   https://github.com/stakx-io/stakx/blob/master/LICENSE.md MIT
6
 */
7
8
namespace allejo\stakx\Manager;
9
10
use allejo\stakx\Filesystem\File;
11
use allejo\stakx\Filesystem\FileExplorerDefinition;
12
use allejo\stakx\Filesystem\FilesystemLoader as fs;
13
use allejo\stakx\Filesystem\Folder;
14
use allejo\stakx\Filesystem\WritableFolder;
15
use allejo\stakx\Service;
16
use Psr\Log\LoggerInterface;
17
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18
19
class AssetManager extends TrackingManager
20
{
21
    /**
22
     * The location of where to write files to.
23
     *
24
     * @var WritableFolder
25
     */
26
    protected $outputDirectory;
27
28
    /**
29
     * Files or patterns to exclude from copying.
30
     *
31
     * @var array
32
     */
33
    protected $excludes;
34
35
    /**
36
     * Files or patterns to ensure are copied regardless of excluded patterns.
37
     *
38
     * @var array
39
     */
40
    protected $includes;
41
42
    /** @var array<string, File> */
43
    protected $explicitAssets;
44
45
    protected $eventDispatcher;
46
    protected $logger;
47
48 95
    public function __construct(EventDispatcherInterface $eventDispatcher, LoggerInterface $logger)
49
    {
50 95
        $this->explicitAssets = [];
51 95
        $this->eventDispatcher = $eventDispatcher;
52 95
        $this->logger = $logger;
53 95
    }
54
55
    /**
56
     * @param string $permalink
57
     * @param File   $file
58
     */
59
    public function addExplicitAsset($permalink, File $file)
60
    {
61
        $this->explicitAssets[$permalink] = $file;
62
    }
63
64
    /**
65
     * @param string $permalink
66
     *
67
     * @return File|null
68
     */
69
    public function getExplicitAsset($permalink)
70
    {
71
        if (isset($this->explicitAssets[$permalink]))
72
        {
73
            return $this->explicitAssets[$permalink];
74
        }
75
76
        return null;
77
    }
78
79
    public function configureFinder($includes = [], $excludes = [])
80
    {
81
        $this->excludes = $excludes;
82
        $this->includes = $includes;
83
    }
84
85
    /**
86
     * Set the target directory of where files should be written to.
87
     *
88
     * @param WritableFolder $directory
89
     */
90
    public function setFolder($directory)
91
    {
92
        $this->outputDirectory = $directory;
93
    }
94
95
    /**
96
     * Copy all of the assets.
97
     */
98
    public function copyFiles()
99
    {
100
        $this->logger->notice('Copying manual assets...');
101
102
        foreach ($this->explicitAssets as $targetPath => $manualAsset)
103
        {
104
            $this->handleTrackableItem($manualAsset, [
105
                'prefix' => '',
106
                'siteTargetPath' => $targetPath,
107
            ]);
108
        }
109
110
        $this->logger->notice('Copying asset files...');
111
112
        $folder = new Folder(Service::getWorkingDirectory());
113
        $def = new FileExplorerDefinition($folder);
114
        $def->includes = $this->includes;
115
        $def->excludes = array_merge(
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge(array('_themes'), $this->excludes) of type array is incompatible with the declared type array<integer,string> of property $excludes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
116
            ['_themes'],
117
            $this->excludes
118
        );
119
120
        $this->scanTrackableItems($def, [
121
            'prefix' => '',
122
        ]);
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function refreshItem($filePath)
129
    {
130
        return $this->handleTrackableItem($filePath, [
131
            'prefix' => '',
132
        ]);
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function shouldBeTracked($filePath)
139
    {
140
        return $this->fileExplorer->matchesPattern($filePath);
141
    }
142
143
    /**
144
     * {@inheritdoc}
145
     */
146
    public function createNewItem($filePath)
147
    {
148
        return $this->handleTrackableItem($filePath, [
149
            'prefix' => '',
150
        ]);
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    protected function handleTrackableItem(File $file, array $options = [])
157
    {
158
        if (!$file->exists())
159
        {
160
            return;
161
        }
162
163
        $filePath = $file->getRealPath();
164
        $pathToStrip = fs::appendPath(Service::getWorkingDirectory(), $options['prefix']);
165
166
        if (isset($options['siteTargetPath']))
167
        {
168
            $siteTargetPath = $options['siteTargetPath'];
169
        }
170
        else
171
        {
172
            $siteTargetPath = ltrim(str_replace($pathToStrip, '', $filePath), DIRECTORY_SEPARATOR);
173
        }
174
175
        try
176
        {
177
            $this->addFileToTracker($file);
178
            $this->saveTrackerOptions($file->getRelativeFilePath(), $options);
179
180
            $this->outputDirectory->copyFile($filePath, $siteTargetPath);
181
            $this->logger->info('Copying file: {file}...', [
182
                'file' => $file->getRelativeFilePath(),
183
            ]);
184
        }
185
        catch (\Exception $e)
186
        {
187
            $this->logger->error($e->getMessage());
188
        }
189
    }
190
}
191