Completed
Pull Request — master (#11)
by Vladimir
02:26
created

AssetManager::handleTrackableItem()   B

Complexity

Conditions 5
Paths 22

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 23
nc 22
nop 2
dl 0
loc 40
rs 8.439
c 0
b 0
f 0
1
<?php
2
3
namespace allejo\stakx\Manager;
4
5
use allejo\stakx\System\Folder;
6
use Symfony\Component\Finder\SplFileInfo;
7
8
class AssetManager extends TrackingManager
9
{
10
    /**
11
     * The location of where to write files to
12
     *
13
     * @var Folder
14
     */
15
    protected $outputDirectory;
16
17
    /**
18
     * Files or patterns to exclude from copying
19
     *
20
     * @var array
21
     */
22
    protected $excludes;
23
24
    /**
25
     * Files or patterns to ensure are copied regardless of excluded patterns
26
     *
27
     * @var array
28
     */
29
    protected $includes;
30
31
    public function configureFinder ($includes = array(), $excludes = array())
32
    {
33
        $this->excludes = $excludes;
34
        $this->includes = $includes;
35
    }
36
37
    /**
38
     * Set the target directory of where files should be written to
39
     *
40
     * @param Folder $directory
41
     */
42
    public function setFolder ($directory)
43
    {
44
        $this->outputDirectory = $directory;
45
    }
46
47
    /**
48
     * Copy all of the assets
49
     */
50
    public function copyFiles()
51
    {
52
        $this->scanTrackableItems(
53
            '.',
54
            array(
55
                'prefix' => ''
56
            ),
57
            $this->includes,
58
            $this->excludes
59
        );
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    protected function handleTrackableItem($file, $options = array())
66
    {
67
        if (is_string($file))
68
        {
69
            $file = $this->fs->appendPath($options['prefix'], $file);
70
        }
71
72
        if (!$this->fs->exists($file)) { return; }
0 ignored issues
show
Bug introduced by
It seems like $file defined by parameter $file on line 65 can also be of type object<Symfony\Component\Finder\SplFileInfo>; however, Symfony\Component\Filesystem\Filesystem::exists() does only seem to accept string|array|object<Traversable>, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
73
74
        if (!($file instanceof SplFileInfo))
75
        {
76
            $file = new SplFileInfo(
77
                $this->fs->absolutePath($file),
78
                $this->fs->getFolderPath($file),
79
                $file
80
            );
81
        }
82
83
        $filePath = $file->getRealPath();
84
        $pathToStrip = $this->fs->appendPath(getcwd(), $options['prefix']);
85
        $siteTargetPath = ltrim(str_replace($pathToStrip, "", $filePath), DIRECTORY_SEPARATOR);
86
87
        try
88
        {
89
            $this->addArrayToTracker(
90
                $file->getRelativePathname(),
91
                array(),
92
                $file->getRelativePathname()
93
            );
94
            $this->saveOptions($file->getRelativePathname(), $options);
95
            $this->outputDirectory->copyFile($filePath, $siteTargetPath);
96
            $this->output->info('Copying file: {file}...', array(
97
                'file' => $file->getRelativePathname()
98
            ));
99
        }
100
        catch (\Exception $e)
101
        {
102
            $this->output->error($e->getMessage());
103
        }
104
    }
105
}