Completed
Push — master ( 9fe6cd...62e6b2 )
by Matthijs
9s
created

getFileSystemFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 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\PersistenceHandler\FilePersistenceHandler;
11
use VDB\Spider\Resource;
12
13
class FileSerializedResourcePersistenceHandler extends FilePersistenceHandler implements PersistenceHandlerInterface
14
{
15
    public function persist(Resource $resource)
16
    {
17
        $path = $this->getResultPath() . $this->getFileSystemPath($resource);
18
        if (! is_dir($path)) {
19
            mkdir($path, 0777, true);
20
        }
21
        $file = new \SplFileObject($path . DIRECTORY_SEPARATOR . $this->getFileSystemFilename($resource), 'w');
22
        $this->totalSizePersisted += $file->fwrite(serialize($resource));
23
    }
24
25
    protected function getFileSystemFilename($resource)
26
    {
27
        $fullPath = $resource->getUri()->getPath();
28
        return urlencode(basename($fullPath));
29
    }
30
31
    protected function getFileSystemPath($resource)
32
    {
33
        $hostname = $resource->getUri()->getHost();
34
        $fullPath = $resource->getUri()->getPath();
35
        return $hostname . dirname($fullPath);
36
    }
37
38
    /**
39
     * @return Resource
40
     */
41
    public function current()
42
    {
43
        return unserialize($this->getIterator()->current()->getContents());
44
    }
45
}
46