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

EventStartDate::parse()   B

Complexity

Conditions 7
Paths 6

Size

Total Lines 25

Duplication

Lines 25
Ratio 100 %

Importance

Changes 0
Metric Value
dl 25
loc 25
rs 8.5866
c 0
b 0
f 0
cc 7
nc 6
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
17 View Code Duplication
class EventStartDate
0 ignored issues
show
Duplication introduced by
This class 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...
18
{
19
    protected $timezone;
20
21
    public function __construct(DateTimeZone $timezone)
22
    {
23
        $this->timezone = $timezone;
24
    }
25
26
    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...
27
    {
28
        // This expression does not work. It looks like the reason is the array-notation...
29
        //$startDate = $xpath->query('//div[contains(text()[2], "event starts")]/following-sibling::h2');
30
        $startDate = $xpath->query('//div[contains(., "event starts")]');
31
32
        if (! $startDate || $startDate->length == 0) {
33
            // This expression does not work. It looks like the reason is the array-notation...
34
            //$startDate = $xpath->query('//div[contains(text()[2], "event date")]/following-sibling::h2');
35
            $startDate = $xpath->query('//div[contains(., "event date")]');
36
        }
37
        if (! $startDate || $startDate->length == 0) {
38
            throw new \InvalidArgumentException('The Event does not seem to have a start date-identifier');
39
        }
40
41
        $startDate = $xpath->query('h2', $startDate->item($startDate->length-1)->parentNode);
42
43
        if (! $startDate || $startDate->length == 0) {
44
            throw new \InvalidArgumentException('The Event does not seem to have a start date');
45
        }
46
47
        $startDate = $startDate->item(0)->textContent;
48
49
        return new DateTimeImmutable($startDate, $this->timezone);
50
    }
51
}
52