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

ObjectWriter   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 14.46 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 86.21%

Importance

Changes 3
Bugs 1 Features 0
Metric Value
wmc 12
c 3
b 1
f 0
lcom 1
cbo 2
dl 12
loc 83
ccs 25
cts 29
cp 0.8621
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A prepare() 0 4 1
A write() 0 4 1
A finish() 0 4 1
A writeItem() 0 10 1
A convert() 0 11 2
A __construct() 0 8 2
A setObjectFactory() 12 12 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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