MediaScanner::getMedias()   B
last analyzed

Complexity

Conditions 6
Paths 39

Size

Total Lines 59
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 40
CRAP Score 6.0005

Importance

Changes 0
Metric Value
eloc 43
dl 0
loc 59
ccs 40
cts 41
cp 0.9756
rs 8.6097
c 0
b 0
f 0
cc 6
nc 39
nop 2
crap 6.0005

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Soluble\MediaTools\Cli\Media;
6
7
use ScriptFUSION\Byte\ByteFormatter;
8
use Soluble\MediaTools\Cli\Exception\MissingFFProbeBinaryException;
9
use Soluble\MediaTools\Video\Exception as VideoException;
10
use Soluble\MediaTools\Video\SeekTime;
11
use Soluble\MediaTools\Video\VideoInfoReaderInterface;
12
use SplFileInfo;
13
14
class MediaScanner
15
{
16
    /** @var VideoInfoReaderInterface */
17
    private $reader;
18
19 3
    public function __construct(VideoInfoReaderInterface $videoInfoReader)
20
    {
21 3
        $this->reader = $videoInfoReader;
22 3
    }
23
24
    /**
25
     * @param SplFileInfo[] $files
26
     */
27 3
    public function getMedias(array $files, ?callable $callback): array
28
    {
29 3
        $bitRateFormatter = new ByteFormatter();
30 3
        $sizeFormatter    = new ByteFormatter();
31
32 3
        $rows      = [];
33 3
        $warnings  = [];
34 3
        $totalSize = 0;
35
36
        /** @var SplFileInfo $file */
37 3
        foreach ($files as $file) {
38 3
            $videoFile = $file->getPathname();
39
40
            try {
41 3
                $info     = $this->reader->getInfo($videoFile);
42 2
                $vStream  = $info->getVideoStreams()->getFirst();
43 2
                $aStream  = $info->getAudioStreams()->getFirst();
44 2
                $pixFmt   = $vStream->getPixFmt();
45 2
                $bitRate  = $vStream->getBitRate();
46 2
                $fileSize = $file->getSize();
47 2
                $frames   = $vStream->getNbFrames();
48
49 2
                $fps = (string) ($vStream->getFps(0) ?? '');
50
51
                $row = [
52 2
                    'file'       => $file,
53 2
                    'duration'   => preg_replace('/\.([0-9])+$/', '', SeekTime::convertSecondsToHMSs(round($info->getDuration(), 1))),
54 2
                    'frames'     => $frames,
55 2
                    'total_time' => $info->getDuration(),
56 2
                    'codec'      => sprintf('%s/%s', $vStream->getCodecName(), $aStream->getCodecName()),
57 2
                    'resolution' => sprintf(
58 2
                        '%sx%s',
59 2
                        $vStream->getWidth(),
60 2
                        $vStream->getHeight()
61
                    ),
62 2
                    'fps'     => $fps,
63 2
                    'bitrate' => ($bitRate > 0 ? $bitRateFormatter->format((int) $bitRate) . '/s' : ''),
64 2
                    'size'    => $sizeFormatter->format($fileSize),
65 2
                    'pixFmt'  => $pixFmt,
66
                ];
67 2
                $rows[] = $row;
68 2
                $totalSize += $fileSize;
69 3
            } catch (VideoException\MissingFFProbeBinaryException $e) {
70 1
                throw new MissingFFProbeBinaryException('Unable to run ffprobe binary, check your config and ensure it\'s properly installed');
71 2
            } catch (VideoException\InfoReaderExceptionInterface $e) {
72 2
                $warnings[] = [$videoFile];
73
            }
74
75 2
            if ($callback === null) {
76
                continue;
77
            }
78
79 2
            $callback();
80
        }
81
82
        return [
83 2
            'rows'      => $rows,
84 2
            'totalSize' => $totalSize,
85 2
            'warnings'  => $warnings,
86
        ];
87
    }
88
}
89