Styler   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 7
eloc 26
c 2
b 0
f 0
dl 0
loc 87
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getStyleTag() 0 5 1
A getStylesheetPath() 0 3 1
A getCSS() 0 3 1
A getStylesheetTag() 0 5 1
A __construct() 0 18 2
A getStylesheetURL() 0 6 1
1
<?php
2
/**
3
 * File containing the {@see Styler} class.
4
 *
5
 * @package Diff
6
 * @subpackage Styler
7
 * @see Styler
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mistralys\Diff\Styler;
13
14
use AppUtils\FileHelper;
15
use AppUtils\FileHelper_Exception;
16
use Mistralys\Diff\DiffException;
17
18
/**
19
 * Utility used to access the CSS styles needed to make the
20
 * HTML highlighting of the HTML diffs possible.
21
 *
22
 * @package Diff
23
 * @subpackage Styler
24
 * @author Sebastian Mordziol <[email protected]>
25
 */
26
class Styler
27
{
28
    public const ERROR_CSS_FILE_NOT_FOUND = 66801;
29
    
30
    private string $path;
31
    private string $fileName = 'styles.css';
32
33
    /**
34
     * @throws DiffException
35
     */
36
    public function __construct()
37
    {
38
        $folder = sprintf(__DIR__.'/../../css/%s', $this->fileName);
39
        $path = realpath($folder);
40
        
41
        if($path === false)
42
        {
43
            throw new DiffException(
44
                'Could not find the highlight CSS file',
45
                sprintf(
46
                    'Tried looking in folder [%s].',
47
                    $folder
48
                ),
49
                self::ERROR_CSS_FILE_NOT_FOUND
50
            );
51
        }
52
        
53
        $this->path = $path;
54
    }
55
56
    /**
57
     * Retrieves the raw CSS source for the highlighting.
58
     *
59
     * @return string
60
     * @throws FileHelper_Exception
61
     */
62
    public function getCSS() : string
63
    {
64
        return FileHelper::readContents($this->path);
65
    }
66
67
    /**
68
     * Retrieves a fully formed `code` tag with the CSS,
69
     * to inject inline into an HTML document.
70
     *
71
     * @return string
72
     * @throws FileHelper_Exception
73
     */
74
    public function getStyleTag() : string
75
    {
76
        return sprintf(
77
            '<!-- Diff highlight CSS --><style>%s</style>',
78
            $this->getCSS()
79
        );
80
    }
81
    
82
   /**
83
    * Retrieves the path to the stylesheet file.
84
    * 
85
    * @return string
86
    */
87
    public function getStylesheetPath() : string
88
    {
89
        return $this->path;
90
    }
91
    
92
   /**
93
    * Retrieves the URL to the stylesheet file, given the
94
    * local URL to the application's vendor folder.
95
    *  
96
    * @param string $vendorURL The URL to the vendor folder (must be accessible in the webroot).
97
    * @return string
98
    */
99
    public function getStylesheetURL(string $vendorURL) : string
100
    {
101
        return sprintf(
102
            '%s/mistralys/text-diff/css/%s',
103
            rtrim($vendorURL, '/'),
104
            $this->fileName
105
        );
106
    }
107
    
108
    public function getStylesheetTag(string $vendorURL) : string
109
    {
110
        return sprintf(
111
            '<link rel="stylesheet" src="%s">',
112
            $this->getStylesheetURL($vendorURL)
113
        );
114
    }
115
}
116