Driver::__call()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This Driver is a Laravel integration for the package php-mattermost-driver
5
 * (https://github.com/gnello/php-mattermost-driver)
6
 *
7
 * For the full copyright and license information, please read the LICENSE.txt
8
 * file that was distributed with this source code. For the full list of
9
 * contributors, visit https://github.com/gnello/laravel-mattermost-driver/contributors
10
 *
11
 * God bless this mess too.
12
 *
13
 * @author Luca Agnello <[email protected]>
14
 * @link https://api.mattermost.com/
15
 */
16
17
namespace Gnello\Mattermost\Laravel;
18
19
use Gnello\Mattermost\Driver as Mattermost;
20
use Illuminate\Support\Arr;
21
use Pimple\Container;
22
23
/**
24
 * Class Driver
25
 *
26
 * @package Gnello\Mattermost\Laravel
27
 */
28
class Driver
29
{
30
    /**
31
     * The application instance.
32
     *
33
     * @var \Illuminate\Foundation\Application
0 ignored issues
show
Bug introduced by
The type Illuminate\Foundation\Application was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
34
     */
35
    protected $app;
36
37
    /**
38
     * The active server instances.
39
     *
40
     * @var array
41
     */
42
    protected $servers = [];
43
44
    /**
45
     * Driver constructor.
46
     *
47
     * @param $app
48
     */
49
    public function __construct($app)
50
    {
51
        $this->app = $app;
52
    }
53
54
    /**
55
     * @param $name
56
     * @return Mattermost
57
     */
58
    public function server($name = null)
59
    {
60
        $name = $this->parseServerName($name);
61
62
        // If we haven't created this server connection, we'll create it based on
63
        // the config provided in the application.
64
        if (!isset($this->servers[$name])) {
65
            $this->servers[$name] = $this->makeConnection($name);
66
        }
67
68
        return $this->servers[$name];
69
    }
70
71
    /**
72
     * Parse the server name.
73
     *
74
     * @param  string  $name
75
     * @return string
76
     */
77
    protected function parseServerName($name)
78
    {
79
        return $name ?: $this->getDefaultServer();
80
    }
81
82
    /**
83
     * Get the default server name.
84
     *
85
     * @return string
86
     */
87
    public function getDefaultServer()
88
    {
89
        return $this->app['config']['mattermost.default'];
90
    }
91
92
    /**
93
     * Get the configuration for a server.
94
     *
95
     * @param  string  $name
96
     * @return array
97
     *
98
     * @throws \InvalidArgumentException
99
     */
100
    protected function configuration($name)
101
    {
102
        $name = $name ?: $this->getDefaultServer();
103
104
        // To get the server configuration, we will just pull each of the
105
        // server configurations and get the configurations for the given name.
106
        // If the configuration doesn't exist, we'll throw an exception and bail.
107
        $servers = $this->app['config']['mattermost.servers'];
108
109
        if (is_null($config = Arr::get($servers, $name))) {
110
            throw new \InvalidArgumentException("Server [$name] not configured.");
111
        }
112
113
        return $config;
114
    }
115
116
    /**
117
     * Make the server connection instance.
118
     *
119
     * @param $name
120
     * @return Mattermost
121
     */
122
    protected function makeConnection($name)
123
    {
124
        $config = $this->configuration($name);
125
             
126
        switch (strtoupper($config['auth']))
127
        {
128
            case 'BEARER':
129
                $options = [
130
                    'driver' => [
131
                        'url' => $config['host'],
132
                        'token' => $config['token'],
133
                    ],
134
                    'guzzle' => $config['guzzle']
135
                ];
136
137
                break;
138
            default:
139
                $options = [
140
                    'driver' => [
141
                        'url' => $config['host'],
142
                        'login_id' => $config['login'],
143
                        'password' => $config['password'],
144
                    ],
145
                    'guzzle' => $config['guzzle']
146
                ];
147
                break;
148
        }
149
        
150
        if (isset($config['scheme'])) {
151
            $options['driver']['scheme'] = $config['scheme'];
152
        }
153
        
154
        $container = new Container($options);
155
156
        $driver = new Mattermost($container);
157
        $driver->authenticate();
158
159
        return $driver;
160
    }
161
162
    /**
163
     * Dynamically pass methods to the default connection.
164
     *
165
     * @param  string  $method
166
     * @param  array   $parameters
167
     * @return mixed
168
     */
169
    public function __call($method, $parameters)
170
    {
171
        return $this->server()->$method(...$parameters);
172
    }
173
}
174