Completed
Push — master ( e0b1d7...ca5e79 )
by Matthijs
10:30 queued 07:23
created

FileSerializedResourcePersistenceHandler::valid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
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 FileSerializedResourcePersistenceHandler 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
    private $path = '';
19
20
    private $spiderId = '';
21
22
    private $totalSizePersisted = 0;
23
24
    /** @var \Iterator */
25
    private $iterator;
26
27
    /** @var Finder */
28
    private $finder;
29
30
    /**
31
     * @param string $path the path where all spider results should be persisted.
32
     *        The results will be grouped in a directory by spider ID.
33
     */
34
    public function __construct($path)
35
    {
36
        $this->path = $path;
37
    }
38
39 View Code Duplication
    public function setSpiderId($spiderId)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
40
    {
41
        $this->spiderId = $spiderId;
42
43
        // create the path
44
        if (!file_exists($this->getResultPath())) {
45
            mkdir($this->getResultPath(), 0700, true);
46
        }
47
    }
48
49
    public function count()
50
    {
51
        return $this->getFinder()->count();
52
    }
53
54
    private function getResultPath()
55
    {
56
        return $this->path . DIRECTORY_SEPARATOR . $this->spiderId . DIRECTORY_SEPARATOR;
57
    }
58
59
    public function persist(Resource $resource)
60
    {
61
        $fileName = urlencode($resource->getUri()->toString());
62
        $file = new \SplFileObject($this->getResultPath() . $fileName, 'w');
63
        $this->totalSizePersisted += $file->fwrite(serialize($resource));
64
    }
65
66 View Code Duplication
    private function getFinder()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
67
    {
68
        if (!$this->finder instanceof Finder) {
69
            $this->finder = Finder::create()->files()->in($this->getResultPath());
70
        }
71
        return $this->finder;
72
    }
73
74
    private function getIterator()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

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