Passed
Push — master ( ca14c1...ab3ac2 )
by Arnaud
05:20
created

Load::process()   B

Complexity

Conditions 7
Paths 22

Size

Total Lines 47
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 7.0872

Importance

Changes 3
Bugs 1 Features 0
Metric Value
cc 7
eloc 32
c 3
b 1
f 0
nc 22
nop 0
dl 0
loc 47
ccs 29
cts 33
cp 0.8788
crap 7.0872
rs 8.4746
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of Cecil.
7
 *
8
 * Copyright (c) Arnaud Ligny <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Cecil\Step\StaticFiles;
15
16
use Cecil\Step\AbstractStep;
17
use Cecil\Util;
18
use Symfony\Component\Finder\Finder;
19
use wapmorgan\Mp3Info\Mp3Info;
20
21
/**
22
 * Loads static files.
23
 */
24
class Load extends AbstractStep
25
{
26
    /**
27
     * {@inheritdoc}
28
     */
29 1
    public function getName(): string
30
    {
31 1
        return 'Loading static files';
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 1
    public function init(array $options): void
38
    {
39 1
        if (is_dir($this->builder->getConfig()->getStaticPath()) && (bool) $this->config->get('static.load')) {
40 1
            $this->canProcess = true;
41
        }
42
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 1
    public function process(): void
48
    {
49 1
        $files = Finder::create()
50 1
            ->files()
51 1
            ->in($this->config->getStaticPath());
52 1
        if (is_array($this->config->get('static.exclude'))) {
53 1
            $files->notName($this->config->get('static.exclude'));
54
        }
55 1
        $files->sortByName(true);
56 1
        $total = count($files);
57
58 1
        if ($total < 1) {
59
            $message = 'No files';
60
            $this->builder->getLogger()->info($message);
61
62
            return;
63
        }
64
65 1
        if (extension_loaded('exif')) {
66 1
            $this->builder->getLogger()->debug('EXIF extension is loaded');
67
        }
68
69 1
        $staticFiles = [];
70 1
        $count = 0;
71
        /** @var \Symfony\Component\Finder\SplFileInfo $file */
72 1
        foreach ($files as $file) {
73 1
            list($type, $subtype) = Util\File::getMimeType($file->getRealPath());
74 1
            $staticFiles[$count]['file'] = $file->getRelativePathname();
75 1
            $staticFiles[$count]['path'] = Util::joinPath($file->getRelativePathname());
76 1
            $staticFiles[$count]['date'] = (new \DateTime())->setTimestamp($file->getCTime());
77 1
            $staticFiles[$count]['updated'] = (new \DateTime())->setTimestamp($file->getMTime());
78 1
            $staticFiles[$count]['name'] = $file->getBasename();
79 1
            $staticFiles[$count]['basename'] = $file->getBasename('.' . $file->getExtension());
80 1
            $staticFiles[$count]['ext'] = $file->getExtension();
81 1
            if ($subtype == 'jpeg') {
82
                $staticFiles[$count]['exif'] = Util\File::readExif($file->getRealPath());
83
            }
84 1
            if ($type == 'audio') {
85 1
                $staticFiles[$count]['audio'] =  new Mp3Info($file->getRealPath());
86
            }
87 1
            $count++;
88
89 1
            $message = \sprintf('File "%s" loaded', $file->getRelativePathname());
90 1
            $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]);
91
        }
92
93 1
        $this->builder->setStatic($staticFiles);
94
    }
95
}
96