Completed
Pull Request — master (#16)
by Matthijs
12:11
created

FileRawResponsePersistenceHandler::getFinder()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4286
cc 2
eloc 4
nc 2
nop 0
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
class FileRawResponsePersistenceHandler implements PersistenceHandler
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
    private $path = '';
19
20
    private $spiderId = '';
21
22
    private $totalSizePersisted = 0;
23
24
    /** @var \Iterator */
25
    private $iterator;
26
27
    /**
28
     * @param string $path the path where all spider results should be persisted.
29
     *        The results will be grouped in a directory by spider ID.
30
     */
31
    public function __construct($path)
32
    {
33
        $this->path = $path;
34
    }
35
36
    public function setSpiderId($spiderId)
37
    {
38
        $this->spiderId = $spiderId;
39
40
        // create the path
41
        if (!file_exists($this->getResultPath())) {
42
            mkdir($this->getResultPath(), 0700, true);
43
        }
44
    }
45
46
    public function count()
47
    {
48
        return $this->getFinder()->count();
49
    }
50
51
    private function getResultPath()
52
    {
53
        return $this->path . DIRECTORY_SEPARATOR . $this->spiderId . DIRECTORY_SEPARATOR;
54
    }
55
56
    public function persist(Resource $resource)
57
    {
58
        $fileName = urlencode($resource->getUri()->toString());
59
        $file = new \SplFileObject($this->getResultPath() . $fileName, 'w');
60
        $rawResponse = $resource->getResponse()->__toString();
61
        $this->totalSizePersisted += $file->fwrite($rawResponse);
62
    }
63
64
    private function getFinder()
65
    {
66
        if (!$this->finder instanceof Finder) {
67
            $this->finder = Finder::create()->files()->in($this->getResultPath());
0 ignored issues
show
Bug introduced by
The property finder does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
68
        }
69
        return $this->finder;
70
    }
71
72
    private function getIterator()
73
    {
74
        if (!$this->iterator instanceof \Iterator) {
75
            $this->iterator = $this->getFinder()->getIterator();
76
        }
77
        return $this->iterator;
78
    }
79
80
    /**
81
     * @return Resource
82
     */
83
    public function current()
84
    {
85
        return $this->getIterator()->current()->getContents();
86
    }
87
88
    /**
89
     * @return void
90
     */
91
    public function next()
92
    {
93
        $this->getIterator()->next();
94
    }
95
96
    /**
97
     * @return int
98
     */
99
    public function key()
100
    {
101
        return $this->getIterator()->key();
102
    }
103
104
    /**
105
     * @return boolean
106
     */
107
    public function valid()
108
    {
109
        return $this->getIterator()->valid();
110
    }
111
112
    /**
113
     * @return void
114
     */
115
    public function rewind()
116
    {
117
        $this->getIterator()->rewind();
118
    }
119
}
120