Completed
Push — master ( 910289...c5392c )
by Nekrasov
04:44
created

ServiceProvider::getBitrixDbConfig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
namespace Arrilot\BitrixModels;
4
5
use Bitrix\Main\Config\Configuration;
6
use Illuminate\Container\Container;
7
use Illuminate\Pagination\Paginator;
8
use Illuminate\Database\Capsule\Manager as Capsule;
9
10
class ServiceProvider
11
{
12
    /**
13
     * Register the service provider.
14
     *
15
     * @return void
16
     */
17
    public static function register()
18
    {
19
        self::bootstrapIlluminatePagination();
20
        self::bootstrapIlluminateDatabase();
21
    }
22
    
23
    /**
24
     * Bootstrap illuminate/pagination
25
     */
26
    protected static function bootstrapIlluminatePagination()
0 ignored issues
show
Coding Style introduced by
bootstrapIlluminatePagination uses the super-global variable $GLOBALS 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...
Coding Style introduced by
bootstrapIlluminatePagination 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...
27
    {
28
        Paginator::currentPathResolver(function () {
29
            return $GLOBALS['APPLICATION']->getCurPage();
30
        });
31
        
32
        Paginator::currentPageResolver(function ($pageName = 'page') {
33
            $page = $_GET[$pageName];
34
            
35
            if (filter_var($page, FILTER_VALIDATE_INT) !== false && (int)$page >= 1) {
36
                return $page;
37
            }
38
            
39
            return 1;
40
        });
41
    }
42
43
    /**
44
     * Bootstrap illuminate/database
45
     */
46
    protected static function bootstrapIlluminateDatabase()
47
    {
48
        $config = self::getBitrixDbConfig();
0 ignored issues
show
Unused Code introduced by
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
49
        
50
        $capsule = new Capsule(self::instantiateServiceContainer());
51
        $capsule->addConnection([
52
            'driver'    => 'mysql',
53
            'host'      => 'localhost',
54
            'database'  => 'orm_test',
55
            'username'  => 'homestead',
56
            'password'  => 'secret',
57
            'charset'   => 'utf8',
58
            'collation' => 'utf8_unicode_ci',
59
            'prefix'    => '',
60
            'strict'    => false,
61
        ]);
62
63
        $capsule->setAsGlobal();
64
        $capsule->bootEloquent();
65
    }
66
67
    /**
68
     * Instantiate service container if it's not instantiated yet.
69
     */
70
    protected static function instantiateServiceContainer()
71
    {
72
        $container = Container::getInstance();
73
        
74
        if (!$container) {
75
            $container = new Container();
76
            Container::setInstance($container);
77
        }
78
        
79
        return $container;
80
    }
81
    
82
    /**
83
     * Get bitrix database configuration array.
84
     *
85
     * @return array
86
     */
87
    protected static function getBitrixDbConfig()
88
    {
89
        $config = Configuration::getInstance();
90
        $connections = $config->get('connections');
91
92
        return $connections['default'];
93
    }
94
}
95