InteractsWithSwooleTable   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A registerTables() 0 17 4
A bindSwooleTable() 0 8 2
A createTables() 0 4 1
1
<?php
2
3
namespace SwooleTW\Http\Concerns;
4
5
use Illuminate\Contracts\Console\Application as ConsoleApp;
6
use Swoole\Table;
7
use SwooleTW\Http\Table\SwooleTable;
8
9
/**
10
 * Trait InteractsWithSwooleTable
11
 *
12
 * @property \Illuminate\Contracts\Container\Container $container
13
 * @property \Illuminate\Contracts\Container\Container $app
14
 */
15
trait InteractsWithSwooleTable
16
{
17
    /**
18
     * @var \SwooleTW\Http\Table\SwooleTable
19
     */
20
    protected $currentTable;
21
22
    /**
23
     * Register customized swoole talbes.
24
     */
25
    protected function createTables()
26
    {
27
        $this->currentTable = new SwooleTable;
28
        $this->registerTables();
29
    }
30
31
    /**
32
     * Register user-defined swoole tables.
33
     */
34
    protected function registerTables()
35
    {
36
        $tables = $this->container->make('config')->get('swoole_http.tables', []);
37
38
        foreach ($tables as $key => $value) {
39
            $table = new Table($value['size']);
40
            $columns = $value['columns'] ?? [];
41
            foreach ($columns as $column) {
42
                if (isset($column['size'])) {
43
                    $table->column($column['name'], $column['type'], $column['size']);
44
                } else {
45
                    $table->column($column['name'], $column['type']);
46
                }
47
            }
48
            $table->create();
49
50
            $this->currentTable->add($key, $table);
51
        }
52
    }
53
54
    /**
55
     * Bind swoole table to Laravel app container.
56
     */
57
    protected function bindSwooleTable()
58
    {
59
        if (! $this->app instanceof ConsoleApp) {
60
            $this->app->singleton(SwooleTable::class, function () {
61
                return $this->currentTable;
62
            });
63
64
            $this->app->alias(SwooleTable::class, 'swoole.table');
65
        }
66
    }
67
}
68