Completed
Pull Request — master (#2)
by Beñat
02:34
created

LogsTable::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 7
nc 1
nop 2
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 16 and the first side effect is on line 8.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace LIN3S\WPSymfonyForm\Admin\Views\Components;
4
5
use LIN3S\WPSymfonyForm\Admin\Storage\Storage;
6
7
if (!class_exists('WP_List_Table')) {
8
    require_once(ABSPATH . 'wp-admin/includes/class-wp-list-table.php');
9
}
10
11
/**
12
 * Logs table.
13
 *
14
 * @author Beñat Espiña <[email protected]>
15
 */
16
class LogsTable extends \WP_List_Table
17
{
18
    /**
19
     * The storage.
20
     *
21
     * @var Storage
22
     */
23
    private $storage;
24
25
    /**
26
     * The form type name.
27
     *
28
     * @var string
29
     */
30
    private $formType;
31
32
    /**
33
     * Constructor.
34
     *
35
     * @param string  $formType The form type name
36
     * @param Storage $storage  The storage
37
     */
38
    public function __construct($formType, Storage $storage)
39
    {
40
        parent::__construct([
41
            'singular' => __('Log', \WPSymfonyForm::TRANSLATION_DOMAIN),
42
            'plural'   => __('Logs', \WPSymfonyForm::TRANSLATION_DOMAIN),
43
            'ajax'     => false,
44
        ]);
45
46
        $this->formType = $formType;
47
        $this->storage = $storage;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function no_items()
54
    {
55
        _e('No emails sent yet', \WPSymfonyForm::TRANSLATION_DOMAIN);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function column_default($item, $column_name)
62
    {
63
        return $item[$column_name];
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function get_columns()
70
    {
71
        if (null === $properties = $this->storage->properties()) {
72
            return [];
73
        }
74
        $columns = [];
75
        foreach ($this->storage->properties() as $property) {
76
            if ($property === 'formType') {
77
                continue;
78
            }
79
            $columns[$property] = __(ucfirst($property), \WPSymfonyForm::TRANSLATION_DOMAIN);
80
        }
81
82
        return $columns;
83
    }
84
85
    /**
86
     * {@inheritdoc}
87
     */
88
    public function get_sortable_columns()
89
    {
90
        return [
91
            'date'  => ['date', true],
92
            'email' => ['email', true],
93
        ];
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99
    public function prepare_items()
100
    {
101
        $this->_column_headers = $this->get_column_info();
102
103
        $limit = $this->get_items_per_page('logs_per_page', 10);
104
        $offset = $this->get_pagenum();
105
        $this->items = $this->storage->query(['formType' => $this->formType], $limit, $offset);
106
107
        $this->set_pagination_args([
108
            'total_items' => $this->storage->size(),
109
            'per_page'    => $limit,
110
        ]);
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116
    public function display()
0 ignored issues
show
Coding Style introduced by
display uses the super-global variable $_GET which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
117
    {
118
        ?>
119
        <div class="meta-box-sortables ui-sortable">
120
            <form action="" method="get">
121
                <?php
122
                $this->search_box(__('Search', \WPSymfonyForm::TRANSLATION_DOMAIN), 'search-id');
123
                foreach ($_GET as $key => $value) {
124
                    if ('s' !== $key) {
125
                        echo("<input type='hidden' name='$key' value='$value' />");
126
                    }
127
                }
128
                ?>
129
            </form>
130
            <form method="post">
131
                <?php parent::display(); ?>
132
            </form>
133
        </div>
134
        <?php
135
    }
136
}
137