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

ObjectWriter::writeItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
crap 1
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