Completed
Push — master ( 0fb3d7...53ff75 )
by Markus
03:02
created

ObjectWriter::setObjectFactory()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 12
loc 12
ccs 7
cts 8
cp 0.875
rs 9.2
cc 4
eloc 8
nc 3
nop 1
crap 4.0312
1
<?php
2
namespace Mathielen\DataImport\Writer;
3
4
use Ddeboer\DataImport\Writer\WriterInterface;
5
use Mathielen\DataImport\Writer\ObjectWriter\ObjectFactoryInterface;
6
use Mathielen\DataImport\Writer\ObjectWriter\DefaultObjectFactory;
7
8
/**
9
 * Writes data to a given SplObjectStorage
10
 */
11
class ObjectWriter implements WriterInterface
12
{
13
14
    /**
15
     * @var ObjectFactoryInterface
16
     */
17
    private $objectFactory;
18
19
    /**
20
     * @var \SplObjectStorage
21
     */
22
    private $objectStorage;
23
24 5
    public function __construct(\SplObjectStorage $objectStorage, $classOrObjectFactory=null)
25
    {
26 5
        $this->objectStorage = $objectStorage;
27
28 5
        if (!empty($classOrObjectFactory)) {
29 2
            $this->setObjectFactory($classOrObjectFactory);
30
        }
31 5
    }
32
33 2 View Code Duplication
    public function setObjectFactory($classOrObjectFactory)
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...
34
    {
35 2
        if (is_object($classOrObjectFactory) && $classOrObjectFactory instanceof ObjectFactoryInterface) {
36 1
            $objectFactory = $classOrObjectFactory;
37 1
        } elseif (is_string($classOrObjectFactory)) {
38 1
            $objectFactory = new DefaultObjectFactory($classOrObjectFactory);
39
        } else {
40
            throw new \InvalidArgumentException("classOrObjectFactory must not be empty");
41
        }
42
43 2
        $this->objectFactory = $objectFactory;
44 2
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49 2
    public function prepare()
50
    {
51 2
        return $this;
52
    }
53
54
    /**
55
     * (non-PHPdoc)
56
     * @see \Ddeboer\DataImport\Writer\WriterInterface::writeItem()
57
     */
58 5
    public function writeItem(array $item)
59
    {
60
        //convert
61 5
        $objectOrItem = $this->convert($item);
62
63
        //write
64 5
        $this->write($objectOrItem);
65
66 5
        return $this;
67
    }
68
69 5
    private function convert(array $item)
70
    {
71
        //dont convert if no object factory
72 5
        if (!$this->objectFactory) {
73 3
            return $item;
74
        }
75
76 2
        $object = $this->objectFactory->factor($item);
77
78 2
        return $object;
79
    }
80
81
    protected function write($object)
82
    {
83
        $this->objectStorage->attach($object);
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89 2
    public function finish()
90
    {
91 2
        return $this;
92
    }
93
}
94