Completed
Push — master ( dca802...7eb0de )
by Ryan
07:03
created

TableBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php namespace Anomaly\Streams\Platform\Ui\Table;
2
3
use Anomaly\Streams\Platform\Traits\FiresCallbacks;
4
use Anomaly\Streams\Platform\Ui\Table\Command\AddAssets;
5
use Anomaly\Streams\Platform\Ui\Table\Command\BuildTable;
6
use Anomaly\Streams\Platform\Ui\Table\Command\LoadTable;
7
use Anomaly\Streams\Platform\Ui\Table\Command\MakeTable;
8
use Anomaly\Streams\Platform\Ui\Table\Command\PostTable;
9
use Anomaly\Streams\Platform\Ui\Table\Command\SetTableResponse;
10
use Anomaly\Streams\Platform\Ui\Table\Component\Filter\Contract\FilterInterface;
11
use Anomaly\Streams\Platform\Ui\Table\Component\Row\Contract\RowInterface;
12
use Anomaly\Streams\Platform\Ui\Table\Component\View\ViewCollection;
13
use Anomaly\Streams\Platform\Ui\Table\Contract\TableRepositoryInterface;
14
use Illuminate\Contracts\Config\Repository;
15
use Illuminate\Foundation\Bus\DispatchesJobs;
16
use Illuminate\Support\Collection;
17
use Symfony\Component\HttpFoundation\Response;
18
19
/**
20
 * Class TableBuilder
21
 *
22
 * @link          http://anomaly.is/streams-platform
23
 * @author        AnomalyLabs, Inc. <[email protected]>
24
 * @author        Ryan Thompson <[email protected]>
25
 * @package       Anomaly\Streams\Platform\Ui\Table
26
 */
27
class TableBuilder
28
{
29
30
    use DispatchesJobs;
31
    use FiresCallbacks;
32
33
    /**
34
     * The ajax flag.
35
     *
36
     * @var bool
37
     */
38
    protected $ajax = false;
39
40
    /**
41
     * The table model.
42
     *
43
     * @var null|string
44
     */
45
    protected $model = null;
46
47
    /**
48
     * The entries handler.
49
     *
50
     * @var null|string
51
     */
52
    protected $entries = null;
53
54
    /**
55
     * The table repository.
56
     *
57
     * @var null|TableRepositoryInterface
58
     */
59
    protected $repository = null;
60
61
    /**
62
     * The views configuration.
63
     *
64
     * @var array|string
65
     */
66
    protected $views = [];
67
68
    /**
69
     * The filters configuration.
70
     *
71
     * @var array|string
72
     */
73
    protected $filters = [];
74
75
    /**
76
     * The columns configuration.
77
     *
78
     * @var array|string
79
     */
80
    protected $columns = [];
81
82
    /**
83
     * The buttons configuration.
84
     *
85
     * @var array|string
86
     */
87
    protected $buttons = [];
88
89
    /**
90
     * The actions configuration.
91
     *
92
     * @var array|string
93
     */
94
    protected $actions = [];
95
96
    /**
97
     * The table options.
98
     *
99
     * @var array
100
     */
101
    protected $options = [];
102
103
    /**
104
     * The table assets.
105
     *
106
     * @var array
107
     */
108
    protected $assets = [];
109
110
    /**
111
     * The table object.
112
     *
113
     * @var Table
114
     */
115
    protected $table;
116
117
    /**
118
     * Create a new TableBuilder instance.
119
     *
120
     * @param Table $table
121
     */
122
    public function __construct(Table $table)
123
    {
124
        $this->table = $table;
125
    }
126
127
    /**
128
     * Build the table.
129
     *
130
     * @return $this
131
     */
132
    public function build()
133
    {
134
        $this->fire('ready', ['builder' => $this]);
135
136
        $this->dispatch(new BuildTable($this));
137
138
        return $this;
139
    }
140
141
    /**
142
     * Make the table response.
143
     *
144
     * @return $this
145
     */
146
    public function make()
147
    {
148
        $this->build();
149
        $this->post();
150
        $this->response();
151
152
        return $this;
153
    }
154
155
    /**
156
     * Return the table response.
157
     *
158
     * @return $this
159
     */
160
    public function response()
161
    {
162
        if ($this->table->getResponse() === null) {
163
            $this->dispatch(new LoadTable($this));
164
            $this->dispatch(new AddAssets($this));
165
            $this->dispatch(new MakeTable($this));
166
        }
167
168
        return $this;
169
    }
170
171
    /**
172
     * Trigger post operations
173
     * for the table.
174
     *
175
     * @return $this
176
     */
177
    public function post()
178
    {
179
        if (app('request')->isMethod('post')) {
180
            $this->dispatch(new PostTable($this));
181
        }
182
183
        return $this;
184
    }
185
186
    /**
187
     * Render the table.
188
     *
189
     * @return Response
190
     */
191
    public function render()
192
    {
193
        $this->make();
194
195
        if ($this->table->getResponse() === null) {
196
            $this->dispatch(new SetTableResponse($this));
197
        }
198
199
        return $this->table->getResponse();
200
    }
201
202
    /**
203
     * Get the ajax flag.
204
     *
205
     * @return bool
206
     */
207
    public function isAjax()
208
    {
209
        return $this->ajax;
210
    }
211
212
    /**
213
     * Set the ajax flag.
214
     *
215
     * @param $ajax
216
     * @return $this
217
     */
218
    public function setAjax($ajax)
219
    {
220
        $this->ajax = $ajax;
221
222
        return $this;
223
    }
224
225
    /**
226
     * Get the table object.
227
     *
228
     * @return Table
229
     */
230
    public function getTable()
231
    {
232
        return $this->table;
233
    }
234
235
    /**
236
     * Set the table model.
237
     *
238
     * @param string $model
239
     * @return $this
240
     */
241
    public function setModel($model)
242
    {
243
        $this->model = $model;
244
245
        return $this;
246
    }
247
248
    /**
249
     * Get the table model.
250
     *
251
     * @return null|string
252
     */
253
    public function getModel()
254
    {
255
        return $this->model;
256
    }
257
258
    /**
259
     * Get the entries.
260
     *
261
     * @return null|string
262
     */
263
    public function getEntries()
264
    {
265
        return $this->entries;
266
    }
267
268
    /**
269
     * Set the entries.
270
     *
271
     * @param $entries
272
     * @return $this
273
     */
274
    public function setEntries($entries)
275
    {
276
        $this->entries = $entries;
277
278
        return $this;
279
    }
280
281
    /**
282
     * Get the repository.
283
     *
284
     * @return TableRepositoryInterface|null
285
     */
286
    public function getRepository()
287
    {
288
        return $this->repository;
289
    }
290
291
    /**
292
     * Set the repository.
293
     *
294
     * @param TableRepositoryInterface $repository
295
     * @return $this
296
     */
297
    public function setRepository(TableRepositoryInterface $repository)
298
    {
299
        $this->repository = $repository;
300
301
        return $this;
302
    }
303
304
    /**
305
     * Set the views configuration.
306
     *
307
     * @param $views
308
     * @return $this
309
     */
310
    public function setViews($views)
311
    {
312
        $this->views = $views;
313
314
        return $this;
315
    }
316
317
    /**
318
     * Get the views configuration.
319
     *
320
     * @return array
321
     */
322
    public function getViews()
323
    {
324
        return $this->views;
325
    }
326
327
    /**
328
     * Set the filters configuration.
329
     *
330
     * @param $filters
331
     * @return $this
332
     */
333
    public function setFilters($filters)
334
    {
335
        $this->filters = $filters;
336
337
        return $this;
338
    }
339
340
    /**
341
     * Get the filters configuration.
342
     *
343
     * @return array
344
     */
345
    public function getFilters()
346
    {
347
        return $this->filters;
348
    }
349
350
    /**
351
     * Add a column configuration.
352
     *
353
     * @param $column
354
     * @return $this
355
     */
356
    public function addColumn($column)
357
    {
358
        $this->columns[] = $column;
359
360
        return $this;
361
    }
362
363
    /**
364
     * Set the columns configuration.
365
     *
366
     * @param $columns
367
     * @return $this
368
     */
369
    public function setColumns($columns)
370
    {
371
        $this->columns = $columns;
372
373
        return $this;
374
    }
375
376
    /**
377
     * Get the columns configuration.
378
     *
379
     * @return array
380
     */
381
    public function getColumns()
382
    {
383
        return $this->columns;
384
    }
385
386
    /**
387
     * Set the buttons configuration.
388
     *
389
     * @param $buttons
390
     * @return $this
391
     */
392
    public function setButtons($buttons)
393
    {
394
        $this->buttons = $buttons;
395
396
        return $this;
397
    }
398
399
    /**
400
     * Get the buttons configuration.
401
     *
402
     * @return array
403
     */
404
    public function getButtons()
405
    {
406
        return $this->buttons;
407
    }
408
409
    /**
410
     * Set the actions configuration.
411
     *
412
     * @param $actions
413
     * @return $this
414
     */
415
    public function setActions($actions)
416
    {
417
        $this->actions = $actions;
418
419
        return $this;
420
    }
421
422
    /**
423
     * Get the actions configuration.
424
     *
425
     * @return array
426
     */
427
    public function getActions()
428
    {
429
        return $this->actions;
430
    }
431
432
    /**
433
     * The the options.
434
     *
435
     * @return array
436
     */
437
    public function getOptions()
438
    {
439
        return $this->options;
440
    }
441
442
    /**
443
     * Set the options.
444
     *
445
     * @param array $options
446
     * @return $this
447
     */
448
    public function setOptions(array $options)
449
    {
450
        $this->options = array_merge($this->options, $options);
451
452
        return $this;
453
    }
454
455
    /**
456
     * Get an option value.
457
     *
458
     * @param      $key
459
     * @param null $default
460
     * @return mixed
461
     */
462
    public function getOption($key, $default = null)
463
    {
464
        return array_get($this->options, $key, $default);
465
    }
466
467
    /**
468
     * Set an option value.
469
     *
470
     * @param $key
471
     * @param $value
472
     * @return $this
473
     */
474
    public function setOption($key, $value)
475
    {
476
        array_set($this->options, $key, $value);
477
478
        return $this;
479
    }
480
481
    /**
482
     * Get the assets.
483
     *
484
     * @return array
485
     */
486
    public function getAssets()
487
    {
488
        return $this->assets;
489
    }
490
491
    /**
492
     * Set the assets.
493
     *
494
     * @param $assets
495
     * @return $this
496
     */
497
    public function setAssets($assets)
498
    {
499
        $this->assets = $assets;
500
501
        return $this;
502
    }
503
504
    /**
505
     * Add an asset.
506
     *
507
     * @param $collection
508
     * @param $asset
509
     * @return $this
510
     */
511 View Code Duplication
    public function addAsset($collection, $asset)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
512
    {
513
        if (!isset($this->assets[$collection])) {
514
            $this->assets[$collection] = [];
515
        }
516
517
        $this->assets[$collection][] = $asset;
518
519
        return $this;
520
    }
521
522
    /**
523
     * Get the table's stream.
524
     *
525
     * @return \Anomaly\Streams\Platform\Stream\Contract\StreamInterface|null
526
     */
527
    public function getTableStream()
528
    {
529
        return $this->table->getStream();
530
    }
531
532
    /**
533
     * Get the table model.
534
     *
535
     * @return \Anomaly\Streams\Platform\Model\EloquentModel|null
536
     */
537
    public function getTableModel()
538
    {
539
        return $this->table->getModel();
540
    }
541
542
    /**
543
     * Get a table option value.
544
     *
545
     * @param      $key
546
     * @param null $default
547
     * @return mixed
548
     */
549
    public function getTableOption($key, $default = null)
550
    {
551
        return $this->table->getOption($key, $default);
552
    }
553
554
    /**
555
     * Set a table option value.
556
     *
557
     * @param $key
558
     * @param $value
559
     * @return $this
560
     */
561
    public function setTableOption($key, $value)
562
    {
563
        $this->table->setOption($key, $value);
564
565
        return $this;
566
    }
567
568
    /**
569
     * Get the table options.
570
     *
571
     * @return Collection
572
     */
573
    public function getTableOptions()
574
    {
575
        return $this->table->getOptions();
576
    }
577
578
    /**
579
     * Set the table entries.
580
     *
581
     * @param Collection $entries
582
     * @return $this
583
     */
584
    public function setTableEntries(Collection $entries)
585
    {
586
        $this->table->setEntries($entries);
587
588
        return $this;
589
    }
590
591
    /**
592
     * Get the table entries.
593
     *
594
     * @return Collection
595
     */
596
    public function getTableEntries()
597
    {
598
        return $this->table->getEntries();
599
    }
600
601
    /**
602
     * Get the table actions.
603
     *
604
     * @return Component\Action\ActionCollection
605
     */
606
    public function getTableActions()
607
    {
608
        return $this->table->getActions();
609
    }
610
611
    /**
612
     * Get the table filters.
613
     *
614
     * @return Component\Filter\FilterCollection
615
     */
616
    public function getTableFilters()
617
    {
618
        return $this->table->getFilters();
619
    }
620
621
    /**
622
     * Get the table filter.
623
     *
624
     * @param $key
625
     * @return FilterInterface
626
     */
627
    public function getTableFilter($key)
628
    {
629
        return $this->table->getFilter($key);
630
    }
631
632
    /**
633
     * Get a table filter value.
634
     *
635
     * @param      $key
636
     * @param null $default
637
     * @return mixed
638
     */
639
    public function getTableFilterValue($key, $default = null)
640
    {
641
        if ($filter = $this->table->getFilter($key)) {
642
            return $filter->getValue();
643
        }
644
645
        return $default;
646
    }
647
648
    /**
649
     * Get the table views.
650
     *
651
     * @return Component\View\ViewCollection
652
     */
653
    public function getTableViews()
654
    {
655
        return $this->table->getViews();
656
    }
657
658
    /**
659
     * Set the table views.
660
     *
661
     * @param ViewCollection $views
662
     * @return $this
663
     */
664
    public function setTableViews(ViewCollection $views)
665
    {
666
        $this->table->setViews($views);
667
668
        return $this;
669
    }
670
671
    /**
672
     * Return whether the table has an active view.
673
     *
674
     * @return bool
675
     */
676
    public function hasActiveView()
677
    {
678
        return !is_null($this->table->getViews()->active());
679
    }
680
681
    /**
682
     * Return whether the table view is active.
683
     *
684
     * @param $slug
685
     * @return bool
686
     */
687
    public function isActiveView($slug)
688
    {
689
        if ($active = $this->table->getViews()->active()) {
690
            return $active->getSlug() === $slug;
691
        }
692
693
        return false;
694
    }
695
696
    /**
697
     * Add a row to the table.
698
     *
699
     * @param RowInterface $row
700
     * @return $this
701
     */
702
    public function addTableRow(RowInterface $row)
703
    {
704
        $this->table->addRow($row);
705
706
        return $this;
707
    }
708
709
    /**
710
     * Add data to the table.
711
     *
712
     * @param $key
713
     * @param $value
714
     * @return $this
715
     */
716
    public function addTableData($key, $value)
717
    {
718
        $this->table->addData($key, $value);
719
720
        return $this;
721
    }
722
723
    /**
724
     * Set the table response.
725
     *
726
     * @param Response $response
727
     */
728
    public function setTableResponse(Response $response)
729
    {
730
        $this->table->setResponse($response);
731
    }
732
733
    /**
734
     * Get the table response.
735
     *
736
     * @return null|Response
737
     */
738
    public function getTableResponse()
739
    {
740
        return $this->table->getResponse();
741
    }
742
743
    /**
744
     * Get the table content.
745
     *
746
     * @return null|string
747
     */
748
    public function getTableContent()
749
    {
750
        return $this->table->getContent();
751
    }
752
753
    /**
754
     * Get a request value.
755
     *
756
     * @param      $key
757
     * @param null $default
758
     * @return mixed
759
     */
760
    public function getRequestValue($key, $default = null)
0 ignored issues
show
Coding Style introduced by
getRequestValue uses the super-global variable $_REQUEST 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...
761
    {
762
        return array_get($_REQUEST, $this->getOption('prefix') . $key, $default);
763
    }
764
}
765