| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /* | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * This file is part of the Superdesk Web Publisher Content Bundle. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Copyright 2016 Sourcefabric z.ú. and contributors. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * For the full copyright and license information, please see the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * AUTHORS and LICENSE files distributed with this source code. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @copyright 2016 Sourcefabric z.ú | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @license http://www.superdesk.org/license | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | namespace SWP\Bundle\ContentBundle\Hydrator; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use function count; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use SWP\Bundle\ContentBundle\Service\ArticleKeywordAdderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use SWP\Bundle\ContentBundle\Service\ArticleSourcesAdderInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use SWP\Bundle\ContentBundle\Model\ArticleInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use SWP\Component\Bridge\Model\PackageInterface; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | final class ArticleHydrator implements ArticleHydratorInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @var ArticleSourcesAdderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     private $articleSourcesAdder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @var ArticleKeywordAdderInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private $articleKeywordAdder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     public function __construct(ArticleSourcesAdderInterface $articleSourcesAdder, ArticleKeywordAdderInterface $articleKeywordAdder) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->articleSourcesAdder = $articleSourcesAdder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->articleKeywordAdder = $articleKeywordAdder; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function hydrate(ArticleInterface $article, PackageInterface $package): ArticleInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $article->setCode($package->getGuid()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 27 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         if (null === ($body = $package->getBody())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 27 |  |             $body = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 27 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $article->setBody($body); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         if (null !== $package->getSlugline() && null === $article->getSlug()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 12 |  |             $article->setSlug($package->getSlugline()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 | 12 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 12 |  |         $article->setTitle($package->getHeadline()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 12 |  |         $article->setAuthors($package->getAuthors()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 | 12 |  |         $article->setExtra($package->getExtra()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $this->populateSources($article, $package); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 12 |  |         $this->populateKeywords($article, $package); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 12 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 12 |  |         $article->setLocale($package->getLanguage()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 12 |  |         $article->setLead($package->getDescription()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $article->setMetadata($package->getMetadata()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 12 |  |         $article->setRoute($article->getRoute()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 12 |  |         return $article; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     private function populateSources(ArticleInterface $article, PackageInterface $package): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |         if (null === $package->getSource()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 12 |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $this->articleSourcesAdder->add($article, $package->getSource()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 12 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 4 |  |     private function populateKeywords(ArticleInterface $article, PackageInterface $package): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 4 |  |         if (0 === count($package->getKeywords())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 4 |  |             return; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 85 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 87 | 8 |  |         foreach ($article->getKeywords() as $keyword) { | 
            
                                                        
            
                                    
            
            
                | 88 |  |  |             $article->removeKeyword($keyword); | 
            
                                                        
            
                                    
            
            
                | 89 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 91 |  |  |         foreach ($package->getKeywords() as $keyword) { | 
            
                                                        
            
                                    
            
            
                | 92 |  |  |             $this->articleKeywordAdder->add($article, $keyword); | 
            
                                                        
            
                                    
            
            
                | 93 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 95 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 96 |  |  |  |