Completed
Push — master ( ccec40...a46923 )
by Timo
08:45
created

LinkColumn::classes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
3
namespace hamburgscleanest\DataTables\Models\ColumnFormatters;
4
5
use hamburgscleanest\DataTables\Interfaces\ColumnFormatter;
6
use hamburgscleanest\DataTables\Models\DataLink;
7
use Illuminate\Database\Eloquent\Model;
8
9
/**
10
 * Class LinkColumn
11
 * @package hamburgscleanest\DataTables\Models\ColumnFormatters
12
 */
13
class LinkColumn implements ColumnFormatter {
14
15
    /** @var DataLink */
16
    private $_dataLink;
17
18
    /** @var bool */
19
    private $_openNew;
20
21
    /** @var string */
22
    private $_classes;
23
24
    /**
25
     * LinkColumn constructor.
26
     * @param string $url
27
     * @param null|string $classes
28
     * @param bool $openInNewWindow
29
     */
30 3
    public function __construct(string $url, ? string $classes = null, bool $openInNewWindow = false)
31
    {
32 3
        $this->_dataLink = new DataLink($url);
33 3
        $this->_classes = $classes;
34 3
        $this->_openNew = $openInNewWindow;
35 3
    }
36
37
    /**
38
     * @param string $classes
39
     * @return LinkColumn
40
     */
41 1
    public function classes(string $classes) : LinkColumn
42
    {
43 1
        $this->_classes = $classes;
44
45 1
        return $this;
46
    }
47
48
    /**
49
     * Open the link in a new window.
50
     *
51
     * @return LinkColumn
52
     */
53 1
    public function openInNew() : LinkColumn
54
    {
55 1
        $this->_openNew = true;
56
57 1
        return $this;
58
    }
59
60
    /**
61
     * Open the link in the same window (redirect).
62
     *
63
     * @return LinkColumn
64
     */
65 1
    public function openInSame() : LinkColumn
66
    {
67 1
        $this->_openNew = false;
68
69 1
        return $this;
70
    }
71
72
    /**
73
     * @param string $name
74
     * @param string $url
75
     * @return string
76
     */
77 3
    private function _renderLink(string $name, string $url) : string
78
    {
79 3
        return '<a href="' . $url . '"' .
80 3
               ($this->_openNew ? ' target="_blank"' : '') .
81 3
               (!empty($this->_classes) ? (' class="' . $this->_classes . '"') : '') .
82 3
               '>' . $name . '</a>';
83
    }
84
85
    /**
86
     * @param Model $rowModel
87
     * @param string $column
88
     * @return string
89
     */
90 3
    public function format(Model $rowModel, string $column) : string
91
    {
92 3
        return $this->_renderLink($column, $this->_dataLink->generate($rowModel));
93
    }
94
}