Completed
Push — master ( ef5bef...038c42 )
by Arne
02:22
created

StandardIndexBuilder::setLogger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Storeman\IndexBuilder;
4
5
use Psr\Log\LoggerAwareInterface;
6
use Psr\Log\LoggerInterface;
7
use Psr\Log\NullLogger;
8
use Storeman\Exception;
9
use Storeman\Hash\HashContainer;
10
use Storeman\Index\Index;
11
use Storeman\Index\IndexObject;
12
use Symfony\Component\Finder\Finder;
13
use Symfony\Component\Finder\SplFileInfo;
14
15
class StandardIndexBuilder implements IndexBuilderInterface, LoggerAwareInterface
16
{
17
    /**
18
     * @var LoggerInterface
19
     */
20
    protected $logger;
21
22
    public function __construct()
23
    {
24
        $this->logger = new NullLogger();
25
    }
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function setLogger(LoggerInterface $logger): void
31
    {
32
        $this->logger = $logger;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function buildIndex(string $path, array $excludedPathsRegexp = []): Index
39
    {
40
        $this->logger->info(sprintf("Building index using %s for path '%s' (excluded: %s)...", static::class, $path, implode(',', $excludedPathsRegexp) ?: '-'));
41
42
        if (!file_exists($path))
43
        {
44
            throw new \InvalidArgumentException("Given path '{$path}' does not exist.");
45
        }
46
        elseif (!is_dir($path))
47
        {
48
            throw new \InvalidArgumentException("Given path '{$path}' is not a directory.");
49
        }
50
        elseif (!is_readable($path))
51
        {
52
            throw new \InvalidArgumentException("Given directory '{$path}' is not readable.'");
53
        }
54
55
        $finder = new Finder();
56
        $finder->in($path);
57
        $finder->ignoreDotFiles(false);
58
59
        foreach ($excludedPathsRegexp as $excludedPathRegexp)
60
        {
61
            $finder->notPath($excludedPathRegexp);
62
        }
63
64
        $index = new Index();
65
66
        foreach ($finder->directories() as $fileInfo)
67
        {
68
            /** @var SplFileInfo $fileInfo */
69
70
            if ($indexObject = $this->buildIndexObject($path, $fileInfo->getRelativePathname()))
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $indexObject is correct as $this->buildIndexObject(...>getRelativePathname()) (which targets Storeman\IndexBuilder\St...der::buildIndexObject()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
71
            {
72
                $index->addObject($indexObject);
73
            }
74
        }
75
76
        foreach ($finder->files() as $fileInfo)
77
        {
78
            /** @var SplFileInfo $fileInfo */
79
80
            if ($indexObject = $this->buildIndexObject($path, $fileInfo->getRelativePathname()))
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $indexObject is correct as $this->buildIndexObject(...>getRelativePathname()) (which targets Storeman\IndexBuilder\St...der::buildIndexObject()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
81
            {
82
                $index->addObject($indexObject);
83
            }
84
        }
85
86
        return $index;
87
    }
88
89
    /**
90
     * {@inheritdoc}
91
     */
92
    public function buildIndexObject(string $basePath, string $relativePath): ?IndexObject
93
    {
94
        $absolutePath = rtrim($basePath, '/') . '/' . $relativePath;
95
96
        clearstatcache(null, $absolutePath);
97
98
        if (!($stat = @lstat($absolutePath)))
99
        {
100
            throw new Exception("lstat() failed for {$absolutePath}");
101
        }
102
103
        $size = $linkTarget = $hashContainer = null;
104
105
        switch ($stat['mode'] & 0xF000)
106
        {
107
            case 0x4000:
0 ignored issues
show
Coding Style introduced by
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
108
109
                $type = IndexObject::TYPE_DIR;
110
111
                break;
112
113
            case 0x8000:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
114
115
                $type = IndexObject::TYPE_FILE;
116
                $size = $stat['size'];
117
                $hashContainer = new HashContainer();
118
119
                break;
120
121
            case 0xA000:
0 ignored issues
show
Coding Style introduced by
The case body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a case statement must start on the line immediately following the case statement.

switch ($expr) {
case "A":
    doSomething(); //right
    break;
case "B":

    doSomethingElse(); //wrong
    break;

}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
122
123
                $type = IndexObject::TYPE_LINK;
124
                $linkTarget = readlink($absolutePath);
125
126
                if ($linkTarget === false)
127
                {
128
                    $this->logger->notice("Found broken link: {$absolutePath}");
129
130
                    // silently ignore broken links
131
                    return null;
132
                }
133
134
                break;
135
136
            default:
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
137
138
                // sockets, pipes, etc.
139
                return null;
140
        }
141
142
        return new IndexObject($relativePath, $type, $stat['mtime'], $stat['ctime'], $stat['mode'] & 0777, $size, $stat['ino'], $linkTarget, null, $hashContainer);
143
    }
144
}
145