Completed
Push — master ( 665782...a8382c )
by Matthijs
02:49
created

FilePersistenceHandler   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 17
c 2
b 0
f 0
lcom 2
cbo 1
dl 0
loc 140
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setSpiderId() 0 9 2
A getFileSystemFilename() 0 6 1
A getFileSystemPath() 0 7 1
A completePath() 0 8 2
A count() 0 4 1
A getResultPath() 0 4 1
persist() 0 1 ?
A getFinder() 0 7 2
A getIterator() 0 7 2
current() 0 1 ?
A next() 0 4 1
A key() 0 4 1
A valid() 0 4 1
A rewind() 0 4 1
1
<?php
2
/**
3
 * @author Matthijs van den Bos <[email protected]>
4
 * @copyright 2013 Matthijs van den Bos
5
 */
6
7
namespace VDB\Spider\PersistenceHandler;
8
9
use Symfony\Component\Finder\Finder;
10
use VDB\Spider\Resource;
11
12
abstract class FilePersistenceHandler implements PersistenceHandlerInterface
13
{
14
    /**
15
     * @var string the path where all spider results should be persisted.
16
     *             The results will be grouped in a directory by spider ID.
17
     */
18
    protected $path = '';
19
20
    protected $spiderId = '';
21
22
    protected $totalSizePersisted = 0;
23
24
    /** @var \Iterator */
25
    protected $iterator;
26
27
    /** @var Finder */
28
    protected $finder;
29
30
    /** @var string The filename that will be appended for resources that end with a slash */
31
    protected $defaultFilename = 'index.html';
32
33
    /**
34
     * @param string $path the path where all spider results should be persisted.
35
     *        The results will be grouped in a directory by spider ID.
36
     */
37
    public function __construct($path)
38
    {
39
        $this->path = $path;
40
    }
41
42
    public function setSpiderId($spiderId)
43
    {
44
        $this->spiderId = $spiderId;
45
46
        // create the path
47
        if (!file_exists($this->getResultPath())) {
48
            mkdir($this->getResultPath(), 0700, true);
49
        }
50
    }
51
52
    protected function getFileSystemFilename($resource)
53
    {
54
        $fullPath = $this->completePath($resource->getUri()->getPath());
55
56
        return urlencode(basename($fullPath));
57
    }
58
59
    protected function getFileSystemPath($resource)
60
    {
61
        $hostname = $resource->getUri()->getHost();
62
        $fullPath = $this->completePath($resource->getUri()->getPath());
63
64
        return $hostname . dirname($fullPath);
65
    }
66
67
    /**
68
     * @return The path that was provided with a default filenameappended if it is
69
     *         a path ending in a /. This is because we don't want to persist
70
     *         the directories as files. This is similar to wget behaviour.
71
     */
72
    protected function completePath($path)
73
    {
74
        if (substr($path, -1, 1) === '/') {
75
            $path .= $this->defaultFilename;
76
        }
77
78
        return $path;
79
    }
80
81
    public function count()
82
    {
83
        return $this->getFinder()->count();
84
    }
85
86
    protected function getResultPath()
87
    {
88
        return $this->path . DIRECTORY_SEPARATOR . $this->spiderId . DIRECTORY_SEPARATOR;
89
    }
90
91
    abstract public function persist(Resource $resource);
92
93
    /**
94
     * @return Finder
95
     */
96
    protected function getFinder()
97
    {
98
        if (!$this->finder instanceof Finder) {
99
            $this->finder = Finder::create()->files()->in($this->getResultPath());
100
        }
101
        return $this->finder;
102
    }
103
104
    /**
105
     * @return \Iterator
106
     */
107
    protected function getIterator()
108
    {
109
        if (!$this->iterator instanceof \Iterator) {
110
            $this->iterator = $this->getFinder()->getIterator();
0 ignored issues
show
Documentation Bug introduced by Matthijs van den Bos
It seems like $this->getFinder()->getIterator() can also be of type array<integer,object<Sym...nt\Finder\SplFileInfo>>. However, the property $iterator is declared as type object<Iterator>. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
111
        }
112
        return $this->iterator;
113
    }
114
115
    /**
116
     * @return Resource
117
     */
118
    abstract public function current();
119
120
    /**
121
     * @return void
122
     */
123
    public function next()
124
    {
125
        $this->getIterator()->next();
126
    }
127
128
    /**
129
     * @return integer|double|string|boolean|null
130
     */
131
    public function key()
132
    {
133
        return $this->getIterator()->key();
134
    }
135
136
    /**
137
     * @return boolean
138
     */
139
    public function valid()
140
    {
141
        return $this->getIterator()->valid();
142
    }
143
144
    /**
145
     * @return void
146
     */
147
    public function rewind()
148
    {
149
        $this->getIterator()->rewind();
150
    }
151
}
152