FileCheck::check()   C
last analyzed

Complexity

Conditions 11
Paths 8

Size

Total Lines 41
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 11.0908

Importance

Changes 0
Metric Value
cc 11
eloc 21
nc 8
nop 2
dl 0
loc 41
ccs 20
cts 22
cp 0.9091
crap 11.0908
rs 5.2653
c 0
b 0
f 0

How to fix   Complexity   

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
/* Copyright (C) 2017 Michael Giesler
3
 *
4
 * This file is part of Dembelo.
5
 *
6
 * Dembelo is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Dembelo is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License 3 for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License 3
17
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace AdminBundle\Service\TwineImport;
21
22
use DembeloMain\Service\FileHandler;
23
24
/**
25
 * Class FileCheck
26
 */
27
class FileCheck
28
{
29
    /**
30
     * @var string
31
     */
32
    private const OPENING_STRING = '<tw-storydata ';
33
34
    /**
35
     * @param FileHandler $fileHandler
36
     * @param string      $filename
37
     *
38
     * @return bool
39
     *
40
     * @throws \Exception
41
     */
42 6
    public function check(FileHandler $fileHandler, string $filename): bool
43
    {
44 6
        $magicStringLength = strlen(self::OPENING_STRING);
45
46 6
        $peekData = $fileHandler->read(1024);
47
48 6
        if (false === $peekData) {
0 ignored issues
show
introduced by
The condition false === $peekData is always false.
Loading history...
49
            throw new \Exception(sprintf("Failed to read data from file '%s'.", $filename));
50
        }
51
52 6
        $peekDataLength = strlen($peekData);
53
54 6
        if ($peekDataLength <= 0) {
55 1
            throw new \Exception(sprintf("File '%s' seems to be empty.", $filename));
56
        }
57
58 5
        for ($i = 0; $i < $peekDataLength; ++$i) {
59 5
            if ($peekData[$i] === ' ' ||
60 4
                $peekData[$i] === "\n" ||
61 4
                $peekData[$i] === "\r" ||
62 5
                $peekData[$i] === "\t") {
63
                // Consume whitespace.
64 2
                continue;
65
            }
66
67 4
            if ($peekDataLength - $i < $magicStringLength) {
68 1
                throw new \Exception(sprintf("File '%s' isn't a Twine archive file.", $filename));
69
            }
70
71 3
            if (substr($peekData, $i, $magicStringLength) !== self::OPENING_STRING) {
72 1
                throw new \Exception(sprintf("File '%s' isn't a Twine archive file.", $filename));
73
            }
74
75 2
            if ($fileHandler->seek(0) !== 0) {
76
                throw new \Exception(sprintf("Couldn't reset reading position after the magic string in the Twine archive file '%s' was checked.", $filename));
77
            }
78
79 2
            return true;
80
        }
81
82 1
        throw new \Exception(sprintf("File '%s' doesn't seem to be a Twine archive file.", $filename));
83
    }
84
}
85