MediaBlock   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 20
c 1
b 0
f 0
dl 0
loc 79
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 1
A __construct() 0 5 1
A getMediaQuery() 0 3 1
A createFromString() 0 13 2
A getStyles() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace ReliqArts\StyleImporter\CSS;
6
7
use ReliqArts\StyleImporter\Exception\InvalidArgument;
8
9
final class MediaBlock implements Extractable
10
{
11
    public const TOKEN_MEDIA = '@media';
12
    public const TOKEN_OPENING_BRACKET = '{';
13
    public const TOKEN_CLOSING_BRACKET = '}';
14
15
    /**
16
     * @var string
17
     */
18
    private $mediaQuery;
19
20
    /**
21
     * @var string
22
     */
23
    private $styles;
24
25
    /**
26
     * @var string block exactly how it appeared in the source
27
     */
28
    private $raw;
29
30
    /**
31
     * MediaBlock constructor.
32
     *
33
     * @param string $mediaQuery
34
     * @param string $styles
35
     * @param string $raw
36
     */
37
    private function __construct(string $mediaQuery, string $styles, string $raw)
38
    {
39
        $this->mediaQuery = $mediaQuery;
40
        $this->styles = $styles;
41
        $this->raw = $raw;
42
    }
43
44
    /**
45
     * @return string
46
     */
47
    public function __toString(): string
48
    {
49
        return $this->raw;
50
    }
51
52
    /**
53
     * @param string $block
54
     *
55
     * @throws InvalidArgument
56
     *
57
     * @return self
58
     */
59
    public static function createFromString(string $block): self
60
    {
61
        if (empty($block)) {
62
            throw new InvalidArgument('Cannot build media block from empty string.');
63
        }
64
65
        list($mediaQuery, $stylesWithTrailingBracket) = array_map(
66
            'trim',
67
            explode(MediaBlock::TOKEN_OPENING_BRACKET, $block, 2)
68
        );
69
        $styles = trim(substr($stylesWithTrailingBracket, 0, -1));
70
71
        return new self($mediaQuery, $styles, $block);
72
    }
73
74
    /**
75
     * @return string
76
     */
77
    public function getMediaQuery(): string
78
    {
79
        return $this->mediaQuery;
80
    }
81
82
    /**
83
     * @return string
84
     */
85
    public function getStyles(): string
86
    {
87
        return $this->styles;
88
    }
89
}
90