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

DoctrineHandler   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 81
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 76.92%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 7
dl 81
loc 81
ccs 30
cts 39
cp 0.7692
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 5 5 1
A handle() 25 25 2
A flush() 4 4 1
A clear() 4 4 1
A validate() 8 8 2

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
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