Completed
Pull Request — master (#36)
by
unknown
04:01
created

FilePersistenceHandler::persist()

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
    /**
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
    public function setSpiderId($spiderId)
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
    protected function getResultPath()
55
    {
56
        return $this->path . DIRECTORY_SEPARATOR . $this->spiderId . DIRECTORY_SEPARATOR;
57
    }
58
59
    abstract public function persist(Resource $resource);
60
61
    /**
62
     * @return Finder
63
     */
64
    protected function getFinder()
65
    {
66
        if (!$this->finder instanceof Finder) {
67
            $this->finder = Finder::create()->files()->in($this->getResultPath());
68
        }
69
        return $this->finder;
70
    }
71
72
    /**
73
     * @return \Iterator
74
     */
75
    protected function getIterator()
76
    {
77
        if (!$this->iterator instanceof \Iterator) {
78
            $this->iterator = $this->getFinder()->getIterator();
79
        }
80
        return $this->iterator;
81
    }
82
83
    /**
84
     * Encodes file name into suitable format and trims its size.
85
     * File name length is limited 255 chars, on Win32 a whole file path is limited 260 chars.
86
     * @param string $fileName
87
     * @return string
88
     */
89
    protected function encodeFileName($fileName)
90
    {
91
        return md5($fileName);
92
    }
93
94
    /**
95
     * @return Resource
96
     */
97
    abstract public function current();
98
99
    /**
100
     * @return void
101
     */
102
    public function next()
103
    {
104
        $this->getIterator()->next();
105
    }
106
107
    /**
108
     * @return integer|double|string|boolean|null
109
     */
110
    public function key()
111
    {
112
        return $this->getIterator()->key();
113
    }
114
115
    /**
116
     * @return boolean
117
     */
118
    public function valid()
119
    {
120
        return $this->getIterator()->valid();
121
    }
122
123
    /**
124
     * @return void
125
     */
126
    public function rewind()
127
    {
128
        $this->getIterator()->rewind();
129
    }
130
}
131