Completed
Push — develop ( 856d20...606eb3 )
by Abdelrahman
21:18
created

BaseServiceProvider   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 65
rs 10
wmc 11
lcom 1
cbo 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A bind() 0 7 4
A bindAndAlias() 0 8 4
A prepareAlias() 0 9 3
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Support Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Support Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Support\Providers;
17
18
use Illuminate\Support\ServiceProvider;
19
20
abstract class BaseServiceProvider extends ServiceProvider
21
{
22
    /**
23
     * The alias pattern.
24
     *
25
     * @var string
26
     */
27
    protected $aliasPattern = '{{class}}Contract';
28
29
    /**
30
     * Register an IoC binding if it's not already been registered.
31
     *
32
     * @param string               $abstract
33
     * @param \Closure|string|null $concrete
34
     * @param bool                 $shared
35
     * @param bool                 $force
36
     *
37
     * @return void
38
     */
39
    protected function bind($abstract, $concrete = null, $shared = true, $force = false)
40
    {
41
        if (! $this->app->bound($abstract) || $force) {
42
            $concrete = $concrete ?: $abstract;
43
            $this->app->bind($abstract, $concrete, $shared);
44
        }
45
    }
46
47
    /**
48
     * Register an IoC binding whether it's already been registered or not.
49
     *
50
     * @param string               $abstract
51
     * @param \Closure|string|null $concrete
52
     * @param bool                 $shared
53
     * @param string|null          $alias
54
     * @param bool                 $force
55
     *
56
     * @return void
57
     */
58
    protected function bindAndAlias($abstract, $concrete = null, $shared = true, $alias = null, $force = false)
59
    {
60
        if (! $this->app->bound($abstract) || $force) {
61
            $concrete = $concrete ?: $abstract;
62
            $this->app->bind($abstract, $concrete, $shared);
63
            $this->app->alias($abstract, $this->prepareAlias($alias, $concrete));
64
        }
65
    }
66
67
    /**
68
     * Prepare the alias.
69
     *
70
     * @param string|null $alias
71
     * @param mixed       $concrete
72
     *
73
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
74
     */
75
    protected function prepareAlias($alias, $concrete)
76
    {
77
        if (! $alias && ! $concrete instanceof \Closure) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $alias of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
78
            $concrete = str_replace('Repositories', 'Contracts', $concrete);
79
            $alias    = str_replace('{{class}}', $concrete, $this->aliasPattern);
80
        }
81
82
        return $alias;
83
    }
84
}
85