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

FileSerializedResourcePersistenceHandler   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 5
c 4
b 0
f 2
lcom 1
cbo 1
dl 0
loc 33
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A persist() 0 9 2
A getFileSystemFilename() 0 5 1
A getFileSystemPath() 0 6 1
A current() 0 4 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