ServiceWriter::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 5
cts 6
cp 0.8333
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2.0185
1
<?php
2
3
namespace Mathielen\DataImport\Writer;
4
5
use Ddeboer\DataImport\Exception\WriterException;
6
7
/**
8
 * Writes data to a given service.
9
 */
10
class ServiceWriter extends ObjectWriter
11
{
12
    /**
13
     * @var callable
14
     */
15
    private $callable;
16
17 5
    public function __construct(callable $callable, $classOrObjectFactory = null)
18
    {
19 5
        if (!is_callable($callable)) {
20
            throw new \InvalidArgumentException('Cannot call callable');
21
        }
22 5
        $this->callable = $callable;
23
24 5
        parent::__construct(new \SplObjectStorage(), $classOrObjectFactory);
25 5
    }
26
27 5
    protected function write($objectOrItem)
28
    {
29
        try {
30 5
            return call_user_func_array($this->callable, array($objectOrItem));
31
        } catch (\Exception $e) {
32
            throw new WriterException('Could not write item: '.print_r($objectOrItem, true), 0, $e);
33
        }
34
    }
35
}
36