Code Duplication    Length = 81-89 lines in 2 locations

src/TreeHouse/IoBundle/Import/Handler/DoctrineHandler.php 1 location

@@ 12-92 (lines=81) @@
9
use TreeHouse\IoBundle\Model\SourceInterface;
10
use TreeHouse\IoBundle\Source\Manager\CachedSourceManager;
11
12
class DoctrineHandler implements HandlerInterface
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
    public function __construct(CachedSourceManager $sourceManager, ValidatorInterface $validator)
29
    {
30
        $this->sourceManager = $sourceManager;
31
        $this->validator = $validator;
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function handle(FeedItemBag $item)
38
    {
39
        // get source and set the data to it
40
        $source = $this->sourceManager->findSourceByFeedOrCreate(
41
            $item->getFeed(),
42
            $item->getOriginalId(),
43
            $item->getOriginalUrl()
44
        );
45
46
        // save data
47
        $source->setData($item->all());
48
        $source->setOriginalUrl($item->getOriginalUrl());
49
50
        try {
51
            $this->validate($source);
52
            $this->sourceManager->persist($source);
53
            $this->sourceManager->flush($source);
54
55
            return $source;
56
        } catch (\Exception $exception) {
57
            $this->sourceManager->detach($source);
58
59
            throw new FailedItemException($source, $exception->getMessage(), $exception->getCode(), $exception);
60
        }
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66
    public function flush()
67
    {
68
        $this->sourceManager->flush();
69
    }
70
71
    /**
72
     * @inheritdoc
73
     */
74
    public function clear()
75
    {
76
        $this->sourceManager->clear();
77
    }
78
79
    /**
80
     * @param SourceInterface $source
81
     *
82
     * @throws ValidationException
83
     */
84
    protected function validate(SourceInterface $source)
85
    {
86
        $violations = $this->validator->validate($source);
87
88
        if ($violations->count()) {
89
            throw ValidationException::create($violations);
90
        }
91
    }
92
}
93

src/TreeHouse/IoBundle/Scrape/Handler/DoctrineHandler.php 1 location

@@ 12-100 (lines=89) @@
9
use TreeHouse\IoBundle\Scrape\ScrapedItemBag;
10
use TreeHouse\IoBundle\Source\Manager\CachedSourceManager;
11
12
class DoctrineHandler implements HandlerInterface
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
    public function __construct(CachedSourceManager $sourceManager, ValidatorInterface $validator)
29
    {
30
        $this->sourceManager = $sourceManager;
31
        $this->validator = $validator;
32
    }
33
34
    /**
35
     * @inheritdoc
36
     */
37
    public function handle(ScrapedItemBag $item)
38
    {
39
        // get source and set the data to it
40
        $source = $this->findSourceOrCreate($item);
41
        $source->setData($item->all());
42
        $source->setDatetimeLastVisited(new \DateTime());
43
44
        try {
45
            $this->validate($source);
46
            $this->sourceManager->persist($source);
47
            $this->sourceManager->flush($source);
48
49
            return $source;
50
        } catch (\Exception $exception) {
51
            $this->sourceManager->detach($source);
52
53
            throw new FailedItemException($source, $exception->getMessage(), $exception->getCode(), $exception);
54
        }
55
    }
56
57
    /**
58
     * @inheritdoc
59
     */
60
    public function flush()
61
    {
62
        $this->sourceManager->flush();
63
    }
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function clear()
69
    {
70
        $this->sourceManager->clear();
71
    }
72
73
    /**
74
     * @param SourceInterface $source
75
     *
76
     * @throws ValidationException
77
     */
78
    protected function validate(SourceInterface $source)
79
    {
80
        $violations = $this->validator->validate($source);
81
82
        if ($violations->count()) {
83
            throw ValidationException::create($violations);
84
        }
85
    }
86
87
    /**
88
     * @param ScrapedItemBag $item
89
     *
90
     * @return SourceInterface
91
     */
92
    protected function findSourceOrCreate(ScrapedItemBag $item)
93
    {
94
        return $this->sourceManager->findSourceByScraperOrCreate(
95
            $item->getScraper(),
96
            $item->getOriginalId(),
97
            $item->getOriginalUrl()
98
        );
99
    }
100
}
101