Passed
Push — master ( f85738...3d85c1 )
by Peter
04:52
created

TableFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 19
dl 0
loc 59
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 12 2
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Grid\Factory;
6
7
use AbterPhp\Admin\Grid\Factory\Table\BodyFactory;
8
use AbterPhp\Admin\Grid\Factory\Table\HeaderFactory;
9
use AbterPhp\Framework\Grid\Component\Actions;
10
use AbterPhp\Framework\Grid\Table\Table;
11
12
class TableFactory
13
{
14
    const ATTRIBUTE_CLASS = 'class';
15
16
    const ERROR_MSG_BODY_CREATED      = 'Grid table body is already created.';
17
    const ERROR_MSG_HEADER_CREATED    = 'Grid table header is already created.';
18
    const ERROR_MSG_TABLE_CREATED     = 'Grid table is already created.';
19
    const ERROR_MSG_NO_BODY_CREATED   = 'Grid table body is not yet created';
20
    const ERROR_MSG_NO_HEADER_CREATED = 'Grig table header is not yet created';
21
22
    /** @var HeaderFactory */
23
    protected $headerFactory;
24
25
    /** @var BodyFactory */
26
    protected $bodyFactory;
27
28
    /** @var array */
29
    protected $tableAttributes = [
30
        self::ATTRIBUTE_CLASS => 'table table-striped table-hover table-bordered',
31
    ];
32
33
    /** @var array */
34
    protected $headerAttributes = [];
35
36
    /** @var array */
37
    protected $bodyAttributes = [];
38
39
    /**
40
     * TableFactory constructor.
41
     *
42
     * @param HeaderFactory $headerFactory
43
     * @param BodyFactory   $bodyFactory
44
     */
45
    public function __construct(HeaderFactory $headerFactory, BodyFactory $bodyFactory)
46
    {
47
        $this->headerFactory = $headerFactory;
48
        $this->bodyFactory   = $bodyFactory;
49
    }
50
51
    /**
52
     * @param callable[]   $getters
53
     * @param Actions|null $rowActions
54
     * @param array        $params
55
     * @param string       $baseUrl
56
     *
57
     * @return Table
58
     */
59
    public function create(
60
        array $getters,
61
        ?Actions $rowActions,
62
        array $params,
63
        string $baseUrl
64
    ): Table {
65
        $hasActions = $rowActions && count($rowActions) > 0;
66
67
        $header = $this->headerFactory->create($hasActions, $params, $baseUrl);
68
        $body   = $this->bodyFactory->create($getters, $this->bodyAttributes, $rowActions);
69
70
        return new Table($body, $header, [], $this->tableAttributes);
71
    }
72
}
73