Completed
Pull Request — master (#37)
by Andreas
01:34
created

OpeningDate::parse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 15

Duplication

Lines 15
Ratio 100 %

Importance

Changes 0
Metric Value
dl 15
loc 15
rs 9.7666
c 0
b 0
f 0
cc 3
nc 2
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Copyright Andrea Heigl <[email protected]>
7
 *
8
 * Licenses under the MIT-license. For details see the included file LICENSE.md
9
 */
10
namespace Callingallpapers\Subcommands\Sessionize\Parser;
11
12
use DateTimeImmutable;
13
use DateTimeZone;
14
use DOMDocument;
15
use DOMXPath;
16
use InvalidArgumentException;
17
use function str_replace;
18
use function trim;
19
20
class OpeningDate
21
{
22
    const OPENING_STRING = 'CfS opens at';
23
24
    protected $timezone;
25
26
    public function __construct(DateTimeZone $timezone)
27
    {
28
        $this->timezone = $timezone;
29
    }
30
31 View Code Duplication
    public function parse(DOMDocument $dom, DOMXPath $xpath) : DateTimeImmutable
0 ignored issues
show
Unused Code introduced by
The parameter $dom is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by
This method 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...
32
    {
33
        $openingDateHolder = $xpath->query('//div[./div/span[contains(text(), "' . self::OPENING_STRING . '")]]');
34
35
        if (! $openingDateHolder || $openingDateHolder->length == 0) {
36
            throw new InvalidArgumentException('No CfP-Open Date found');
37
        }
38
39
        $openingDay  = $openingDateHolder->item(0)->getElementsByTagName('h2')->item(0)->textContent;
40
        $openingHour = $openingDateHolder->item(0)->getElementsByTagName('span')->item(0)->textContent;
41
42
        $openingHour = $this->clearOpeningHour($openingHour);
43
44
        return new DateTimeImmutable($openingDay. ' ' . $openingHour, $this->timezone);
45
    }
46
47
    private function clearOpeningHour(string $openingHour) : string
48
    {
49
        return trim(str_replace(self::OPENING_STRING, '', $openingHour));
50
    }
51
}
52