Load::init()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Cecil.
5
 *
6
 * (c) Arnaud Ligny <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
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
 * Load static files step.
23
 *
24
 * This step is responsible for loading static files from the configured static path.
25
 * It scans the directory for files, excluding any specified in the configuration.
26
 *
27
 * The loaded files are processed to extract metadata such as file type, subtype,
28
 * modification date.
29
 * If the file is a JPEG image, it extracts EXIF metadata.
30
 * If the file is an audio file, it extracts audio metadata.
31
 * If the file is a video file, it extracts duration and resolution.
32
 */
33
class Load extends AbstractStep
34
{
35
    /**
36
     * {@inheritdoc}
37
     */
38 1
    public function getName(): string
39
    {
40 1
        return 'Loading static files';
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 1
    public function init(array $options): void
47
    {
48 1
        if (is_dir($this->config->getStaticPath()) && $this->config->isEnabled('static.load')) {
49 1
            $this->canProcess = true;
50
        }
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 1
    public function process(): void
57
    {
58 1
        $files = Finder::create()
59 1
            ->files()
60 1
            ->in($this->config->getStaticPath());
61 1
        if (\is_array($exclude = $this->config->get('static.exclude'))) {
62 1
            $files->notName($exclude);
63
        }
64 1
        $files->sortByName(true);
65 1
        $total = \count($files);
66
67 1
        if ($total < 1) {
68
            $message = 'No files';
69
            $this->builder->getLogger()->info($message);
70
71
            return;
72
        }
73
74 1
        if (\extension_loaded('exif')) {
75 1
            $this->builder->getLogger()->debug('EXIF extension is loaded');
76
        }
77
78 1
        $staticFiles = [];
79 1
        $count = 0;
80
        /** @var \Symfony\Component\Finder\SplFileInfo $file */
81 1
        foreach ($files as $file) {
82 1
            list($type, $subtype) = Util\File::getMediaType($file->getRealPath());
83 1
            $staticFiles[$count]['file'] = $file->getRelativePathname();
84 1
            $staticFiles[$count]['path'] = Util::joinPath($file->getRelativePathname());
85 1
            $staticFiles[$count]['date'] = (new \DateTime())->setTimestamp($file->getCTime());
86 1
            $staticFiles[$count]['updated'] = (new \DateTime())->setTimestamp($file->getMTime());
87 1
            $staticFiles[$count]['name'] = $file->getBasename();
88 1
            $staticFiles[$count]['basename'] = $file->getBasename('.' . $file->getExtension());
89 1
            $staticFiles[$count]['ext'] = $file->getExtension();
90 1
            $staticFiles[$count]['type'] = $type;
91 1
            $staticFiles[$count]['subtype'] = $subtype;
92 1
            if ($subtype == 'image/jpeg') {
93
                // read EXIF data for JPEG images
94 1
                $staticFiles[$count]['exif'] = Util\File::readExif($file->getRealPath());
95
            }
96 1
            if ($type == 'audio') {
97
                // read audio metadata
98 1
                $staticFiles[$count]['audio'] = new Mp3Info($file->getRealPath());
99
            }
100 1
            if ($type == 'video') {
101
                // read video metadata
102 1
                $videoInfos = (new \getID3())->analyze($file->getRealPath());
103 1
                $staticFiles[$count]['video'] = [
104 1
                    'duration' => $videoInfos['playtime_seconds'] ?? 0,
105 1
                    'width'    => $videoInfos['video']['resolution_x'] ?? 0,
106 1
                    'height'   => $videoInfos['video']['resolution_y'] ?? 0,
107 1
                ];
108
            }
109 1
            $count++;
110
111 1
            $message = \sprintf('File "%s" loaded', $file->getRelativePathname());
112 1
            $this->builder->getLogger()->info($message, ['progress' => [$count, $total]]);
113
        }
114
115 1
        $this->builder->setStatic($staticFiles);
116
    }
117
}
118