loadImdbReleaseDatesPageHtml()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 0
cts 9
cp 0
rs 9.6666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Movies\Service;
6
7
use App\Movies\Entity\Movie;
8
use Symfony\Component\DomCrawler\Crawler;
9
10
class ImdbReleaseDateParserService
11
{
12
    private const IMDB_RELEASE_DATES_URL = 'https://www.imdb.com/title/{id}/releaseinfo?ref_=tt_dt_dt';
13
14
    private $imdbMapper;
15
16
    public function __construct(ImdbDataMapper $mapper)
17
    {
18
        $this->imdbMapper = $mapper;
19
    }
20
21
    public function getReleaseDates(Movie $movie): array
22
    {
23
        if ($movie->getImdbId() === null) {
24
            return [];
25
        }
26
27
        $html = $this->loadImdbReleaseDatesPageHtml($movie->getImdbId());
28
29
        // Because filterXPath('//*[@id="release_dates"]//td') don't work correctly due errors in html from imdb page
30
        $releasesPosition = mb_strpos($html, 'id="releases"');
31
        if ($releasesPosition === false) {
32
            // no releases yet on imdb
33
            return [];
34
        }
35
        $html = mb_substr($html, $releasesPosition);
36
37
        $akasPosition = mb_strpos($html, 'id="akas"');
38
        if ($akasPosition === false) {
39
            $html = mb_substr($html, 0);
40
        } else {
41
            $html = mb_substr($html, 0, $akasPosition);
42
        }
43
44
        $crawler = new Crawler($html, $this->getEndpoint($movie->getImdbId()));
45
46
        $tds = $crawler->filterXPath('//td')->getIterator();
47
48
        $result = [];
49
        $country = '';
50
        foreach ($tds as $td) {
51
            if ($td->getAttribute('class') === 'release-date-item__date') {
52
                $result[$country] = $this->imdbMapper->dateToObject($td->textContent);
53
                continue;
54
            }
55
56
            if ($td->hasChildNodes() && $td->getElementsByTagName('a')->item(0) !== null) {
57
                $country = $td->getElementsByTagName('a')->item(0)->textContent;
58
                $country = trim($country);
59
                $country = $this->imdbMapper->countryToCode($country);
60
                continue;
61
            }
62
        }
63
64
        return $result;
65
    }
66
67
    private function getEndpoint(string $imdbId): string
68
    {
69
        return str_replace('{id}', $imdbId, self::IMDB_RELEASE_DATES_URL);
70
    }
71
72
    private function loadImdbReleaseDatesPageHtml(string $imdbId): string
73
    {
74
        $endpoint = $this->getEndpoint($imdbId);
75
76
        $c = curl_init($endpoint);
77
        curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
78
79
        $html = curl_exec($c);
80
81
        if ($html === false) {
82
            //todo write to log curl_error()
83
            return '';
84
        }
85
86
        curl_close($c);
87
88
        return $html;
89
    }
90
}
91