Completed
Pull Request — master (#37)
by Peter
23:57
created

DoctrineHandler::handle()   A

Complexity

Conditions 2
Paths 4

Size

Total Lines 25

Duplication

Lines 25
Ratio 100 %

Code Coverage

Tests 15
CRAP Score 2.0373

Importance

Changes 0
Metric Value
dl 25
loc 25
ccs 15
cts 19
cp 0.7895
rs 9.52
c 0
b 0
f 0
cc 2
nc 4
nop 1
crap 2.0373
1
<?php
2
3
namespace TreeHouse\IoBundle\Import\Handler;
4
5
use Symfony\Component\Validator\Validator\ValidatorInterface;
6
use TreeHouse\IoBundle\Exception\ValidationException;
7
use TreeHouse\IoBundle\Import\Exception\FailedItemException;
8
use TreeHouse\IoBundle\Import\Feed\FeedItemBag;
9
use TreeHouse\IoBundle\Model\SourceInterface;
10
use TreeHouse\IoBundle\Source\Manager\CachedSourceManager;
11
12 View Code Duplication
class DoctrineHandler implements HandlerInterface
0 ignored issues
show
Duplication introduced by
This class 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...
13
{
14
    /**
15
     * @var CachedSourceManager
16
     */
17
    protected $sourceManager;
18
19
    /**
20
     * @var ValidatorInterface
21
     */
22
    protected $validator;
23
24
    /**
25
     * @param CachedSourceManager $sourceManager
26
     * @param ValidatorInterface  $validator
27
     */
28 2
    public function __construct(CachedSourceManager $sourceManager, ValidatorInterface $validator)
29
    {
30 2
        $this->sourceManager = $sourceManager;
31 2
        $this->validator = $validator;
32 2
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37 2
    public function handle(FeedItemBag $item)
38
    {
39
        // get source and set the data to it
40 2
        $source = $this->sourceManager->findSourceByFeedOrCreate(
41 2
            $item->getFeed(),
42 2
            $item->getOriginalId(),
43 2
            $item->getOriginalUrl()
44 2
        );
45
46
        // save data
47 2
        $source->setData($item->all());
48 2
        $source->setOriginalUrl($item->getOriginalUrl());
49
50
        try {
51 2
            $this->validate($source);
52 2
            $this->sourceManager->persist($source);
53 2
            $this->sourceManager->flush($source);
54
55 2
            return $source;
56 2
        } catch (\Exception $exception) {
57 2
            $this->sourceManager->detach($source);
58
59 2
            throw new FailedItemException($source, $exception->getMessage(), $exception->getCode(), $exception);
60
        }
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 2
    public function flush()
67
    {
68 2
        $this->sourceManager->flush();
69 2
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74 2
    public function clear()
75
    {
76 2
        $this->sourceManager->clear();
77 2
    }
78
79
    /**
80
     * @param SourceInterface $source
81
     *
82
     * @throws ValidationException
83
     */
84 2
    protected function validate(SourceInterface $source)
85
    {
86 2
        $violations = $this->validator->validate($source);
87
88 2
        if ($violations->count()) {
89 2
            throw ValidationException::create($violations);
90
        }
91 2
    }
92
}
93