Completed
Push — master ( 665782...a8382c )
by Matthijs
04:42 queued 02:50
created

FilePersistenceHandler::current()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 1
nc 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
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