Test Failed
Push — master ( c5c65d...95a748 )
by Sebastian
02:26
created

Mailcode_Styler::getCSS()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * File containing the {@see Mailcode} class.
4
 *
5
 * @package Mailcode
6
 * @subpackage Core
7
 * @see Mailcode
8
 */
9
10
declare(strict_types=1);
11
12
namespace Mailcode;
13
14
use AppUtils\FileHelper;
15
16
/**
17
 * Generat
18
 *
19
 * @package Mailcode
20
 * @subpackage Core
21
 * @author Sebastian Mordziol <[email protected]>
22
 */
23
class Mailcode_Styler
24
{
25
    const ERROR_CSS_FILE_NOT_FOUND = 60901;
26
    
27
   /**
28
    * @var string
29
    */
30
    private $path;
31
    
32
   /**
33
    * @var string
34
    */
35
    private $fileName = 'highlight.css';
36
    
37
    public function __construct()
38
    {
39
        $folder = sprintf(__DIR__.'/../../css/%s', $this->fileName);
40
        $path = realpath($folder);
41
        
42
        if($path === false)
43
        {
44
            throw new Mailcode_Exception(
45
                'Could not find the highlight CSS file',
46
                sprintf(
47
                    'Tried looking in folder [%s].',
48
                    $folder
49
                ),
50
                self::ERROR_CSS_FILE_NOT_FOUND
51
            );
52
        }
53
        
54
        $this->path = $path;
55
    }
56
   
57
   /**
58
    * Retrieves the raw CSS source for the highlighting.
59
    * 
60
    * @return string
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
    */
73
    public function getStyleTag() : string
74
    {
75
        return sprintf(
76
            '<!-- Mailcode highlight CSS --><style>%s</style>',
77
            $this->getCSS()
78
        );
79
    }
80
    
81
   /**
82
    * Retrieves the path to the stylesheet file.
83
    * 
84
    * @return string
85
    */
86
    public function getStylesheetPath() : string
87
    {
88
        return $this->path;
89
    }
90
    
91
   /**
92
    * Retrieves the URL to the stylesheet file, given the
93
    * local URL to the application's vendor folder.
94
    *  
95
    * @param string $vendorURL The URL to the vendor folder (must be accessible in the webroot).
96
    * @return string
97
    */
98
    public function getStylesheetURL(string $vendorURL) : string
99
    {
100
        return sprintf(
101
            '%s/mistralys/mailcode/css/%s',
102
            rtrim($vendorURL, '/'),
103
            $this->fileName
104
        );
105
    }
106
    
107
    public function getStylesheetTag(string $vendorURL) : string
108
    {
109
        return sprintf(
110
            '<link rel="stylesheet" src="%s">',
111
            $this->getStylesheetURL($vendorURL)
112
        );
113
    }
114
}
115