Completed
Push — develop ( 958042...3a11db )
by Abdelrahman
04:40
created

RepositoryServiceProvider::publishResources()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Repository 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 Repository Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Repository\Providers;
17
18
use Illuminate\Support\ServiceProvider;
19
use Rinvex\Repository\Listeners\RepositoryEventListener;
20
21
class RepositoryServiceProvider extends ServiceProvider
22
{
23
    /**
24
     * The repository alias pattern.
25
     *
26
     * @var string
27
     */
28
    protected $repositoryAliasPattern = '{{class}}Contract';
0 ignored issues
show
Comprehensibility Naming introduced by
The variable name $repositoryAliasPattern exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
29
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function register()
34
    {
35
        // Merge config
36
        $this->mergeConfigFrom(realpath(__DIR__.'/../../config/config.php'), 'rinvex.repository');
37
38
        // Register the event listener
39
        $this->app->bind('rinvex.repository.listener', RepositoryEventListener::class);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function boot()
46
    {
47
        // Publish Resources
48
        $this->publishResources();
49
50
        // Subscribe the registered event listener
51
        $this->app['events']->subscribe('rinvex.repository.listener');
52
    }
53
54
    /**
55
     * Publish package resources.
56
     *
57
     * @return void
58
     */
59
    protected function publishResources()
60
    {
61
        // Publish config
62
        $this->publishes([
63
            realpath(__DIR__.'/../../config/config.php') => config_path('rinvex.repository.php'),
64
        ], 'config');
65
    }
66
67
    /**
68
     * Register an IoC binding whether it's already been registered or not.
69
     *
70
     * @param string               $abstract
71
     * @param \Closure|string|null $concrete
72
     * @param bool                 $shared
73
     * @param string|null          $alias
74
     * @param bool                 $force
75
     *
76
     * @return void
77
     */
78
    protected function bindRepository($abstract, $concrete = null, $shared = true, $alias = null, $force = false)
79
    {
80
        if (! $this->app->bound($abstract) || $force) {
81
            $concrete = $concrete ?: $abstract;
82
            $this->app->bind($abstract, $concrete, $shared);
83
            $this->app->alias($abstract, $this->prepareRepositoryAlias($alias, $concrete));
84
        }
85
    }
86
87
    /**
88
     * Prepare the repository alias.
89
     *
90
     * @param string|null $alias
91
     * @param mixed       $concrete
92
     *
93
     * @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...
94
     */
95
    protected function prepareRepositoryAlias($alias, $concrete)
96
    {
97
        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...
98
            $concrete = str_replace('Repositories', 'Contracts', $concrete);
99
            $alias = str_replace('{{class}}', $concrete, $this->repositoryAliasPattern);
100
        }
101
102
        return $alias;
103
    }
104
}
105