Completed
Push — master ( 2319eb...3c4d84 )
by Vladimir
02:22
created

AssetManager::handleTrackableItem()   B

Complexity

Conditions 4
Paths 12

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 32
rs 8.5806
cc 4
eloc 17
nc 12
nop 2
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 allejo\stakx\Filesystem\File;
11
use allejo\stakx\Filesystem\FilesystemLoader as fs;
12
use allejo\stakx\Filesystem\Folder;
13
use Psr\Log\LoggerInterface;
14
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
15
16
class AssetManager extends TrackingManager
17
{
18
    /**
19
     * The location of where to write files to.
20
     *
21
     * @var Folder
22
     */
23
    protected $outputDirectory;
24
25
    /**
26
     * Files or patterns to exclude from copying.
27
     *
28
     * @var array
29
     */
30
    protected $excludes;
31
32
    /**
33
     * Files or patterns to ensure are copied regardless of excluded patterns.
34
     *
35
     * @var array
36
     */
37
    protected $includes;
38
39
    protected $eventDispatcher;
40
    protected $logger;
41
42
    public function __construct(EventDispatcherInterface $eventDispatcher, LoggerInterface $logger)
43
    {
44
        $this->eventDispatcher = $eventDispatcher;
45
        $this->logger = $logger;
46
    }
47
48
    public function configureFinder($includes = array(), $excludes = array())
49
    {
50
        $this->excludes = $excludes;
51
        $this->includes = $includes;
52
    }
53
54
    /**
55
     * Set the target directory of where files should be written to.
56
     *
57
     * @param Folder $directory
58
     */
59
    public function setFolder($directory)
60
    {
61
        $this->outputDirectory = $directory;
62
    }
63
64
    /**
65
     * Copy all of the assets.
66
     */
67
    public function copyFiles()
68
    {
69
        $this->scanTrackableItems(
70
            getcwd(),
71
            array(
72
                'prefix' => '',
73
            ),
74
            $this->includes,
75
            array_merge(
76
                array('_themes'),
77
                $this->excludes
78
            )
79
        );
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85
    public function refreshItem($filePath)
86
    {
87
        return $this->handleTrackableItem($filePath, array(
88
            'prefix' => ''
89
        ));
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function shouldBeTracked($filePath)
96
    {
97
        return $this->fileExplorer->matchesPattern($filePath);
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103
    public function createNewItem($filePath)
104
    {
105
        return $this->handleTrackableItem($filePath, array(
106
            'prefix' => '',
107
        ));
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    protected function handleTrackableItem(File $file, array $options = array())
114
    {
115
        if (is_string($file))
116
        {
117
            $file = ltrim(fs::appendPath($options['prefix'], $file), DIRECTORY_SEPARATOR);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $file. This often makes code more readable.
Loading history...
118
            $file = fs::createFileObject($file);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $file. This often makes code more readable.
Loading history...
119
        }
120
121
        if (!fs::exists($file))
122
        {
123
            return;
124
        }
125
126
        $filePath = $file->getRealPath();
127
        $pathToStrip = fs::appendPath(getcwd(), $options['prefix']);
128
        $siteTargetPath = ltrim(str_replace($pathToStrip, '', $filePath), DIRECTORY_SEPARATOR);
129
130
        try
131
        {
132
            $this->addFileToTracker($file);
133
            $this->saveTrackerOptions($file->getRelativeFilePath(), $options);
134
135
            $this->outputDirectory->copyFile($filePath, $siteTargetPath);
136
            $this->logger->info('Copying file: {file}...', array(
137
                'file' => $file->getRelativeFilePath(),
138
            ));
139
        }
140
        catch (\Exception $e)
141
        {
142
            $this->logger->error($e->getMessage());
143
        }
144
    }
145
}
146