Passed
Push — dev ( 043eb4...bf3609 )
by Dispositif
06:23
created

ArticleFromURL   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 46
dl 0
loc 84
rs 10
c 1
b 0
f 0
wmc 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getResult() 0 3 1
A __construct() 0 9 2
B process() 0 52 7
1
<?php
2
/**
3
 * This file is part of dispositif/wikibot application
4
 * 2019 © Philippe M. <[email protected]>
5
 * For the full copyright and MIT license information, please view the LICENSE file.
6
 */
7
8
declare(strict_types=1);
9
10
namespace App\Domain\Publisher;
11
12
use App\Application\PublisherAction;
13
use App\Domain\Models\Wiki\ArticleOrLienBriseInterface;
14
use App\Domain\WikiTemplateFactory;
15
use Exception;
16
use Throwable;
17
18
/**
19
 * news URL to {{Article}}.
20
 * Class ArticleFromURL
21
 *
22
 * @package App\Application\Examples
23
 */
24
class ArticleFromURL
25
{
26
    private $publisherAction;
27
    /**
28
     * @var ArticleOrLienBriseInterface|null
29
     */
30
    private $article;
31
    /**
32
     * @var string
33
     */
34
    private $url;
35
36
    public function __construct(PublisherAction $publisher)
37
    {
38
        $this->publisherAction = $publisher;
39
        $this->url = $publisher->getUrl();
40
        try {
41
            $this->article = $this->process();
42
        } catch (Exception $e) {
43
            dump($e);
44
            die;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
45
        }
46
    }
47
48
    /**
49
     * @throws Exception
50
     */
51
    private function process(): ?ArticleOrLienBriseInterface
52
    {
53
        $mapper = PublisherMapperFactory::fromURL($this->url);
54
        if (!$mapper) {
55
            return null;
56
        }
57
        sleep(10);
58
        $arrayLD = [];
0 ignored issues
show
Unused Code introduced by
The assignment to $arrayLD is dead and can be removed.
Loading history...
59
        try {
60
            $html = $this->publisherAction->getHTMLSource();
61
            $htmlData = $this->publisherAction->extractWebData($html);
62
        } catch (Throwable $e) {
63
            if (strpos($e->getMessage(), '404') !== false) {
64
                dump('****** lien brisé !!!!');
65
                $lienBrise = WikiTemplateFactory::create('lien brisé');
66
                $lienBrise->hydrate(['url' => $this->url, 'titre' => 'Article de presse', 'brisé le' => date('d-m-Y')]);
67
68
                return $lienBrise; // ok
0 ignored issues
show
Bug Best Practice introduced by
The expression return $lienBrise could return the type App\Domain\Models\Wiki\G...ls\Wiki\OuvrageTemplate which is incompatible with the type-hinted return App\Domain\Models\Wiki\A...LienBriseInterface|null. Consider adding an additional type-check to rule them out.
Loading history...
69
            }
70
            echo "*** Erreur ".$e->getMessage()."\n";
71
72
            return null;
73
        }
74
75
        if (empty($htmlData)) {
76
            echo "*** Pas de data Json-LD ou meta\n";
77
78
            return null;
79
        }
80
        $htmlData['url'] = $this->url;
81
82
        try {
83
            $articleData = $mapper->process($htmlData);
84
        } catch (Throwable $e) {
85
            echo sprintf(
86
                "SKIP : %s %s:%s \n",
87
                $e->getMessage(),
88
                $e->getFile(),
89
                $e->getLine()
90
            );
91
92
            return null;
93
        }
94
95
        if (!empty($articleData)) {
96
            $article = WikiTemplateFactory::create('article');
97
            $article->hydrate($articleData);
98
99
            return $article; // ok
0 ignored issues
show
Bug Best Practice introduced by
The expression return $article could return the type App\Domain\Models\Wiki\G...ls\Wiki\OuvrageTemplate which is incompatible with the type-hinted return App\Domain\Models\Wiki\A...LienBriseInterface|null. Consider adding an additional type-check to rule them out.
Loading history...
100
        }
101
102
        return null;
103
    }
104
105
    public function getResult(): ?ArticleOrLienBriseInterface
106
    {
107
        return $this->article;
108
    }
109
110
}
111