Passed
Push — master ( c7e265...e8c097 )
by Xavier
03:54
created

AbstractController::debug()   A

Complexity

Conditions 1
Paths 0

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 0
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Controller;
4
5
use GuzzleHttp\Client;
6
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
7
use Symfony\Component\DependencyInjection\ContainerInterface;
8
9
 abstract class AbstractController extends Controller
10
{
11
12
    /**
13
     * Guzzle HTTP client instance linked to the OpenLRW API
14
     *
15
     * @var Client
16
     */
17
    protected $http;
18
19
    /**
20
     * Constructor.
21
     * @param ContainerInterface $container
22
     */
23
    public function __construct(ContainerInterface $container)
0 ignored issues
show
Unused Code introduced by
The parameter $container is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

23
    public function __construct(/** @scrutinizer ignore-unused */ ContainerInterface $container)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
24
    {
25
        $this->http = new Client(['base_uri' => env('API_URI')]);
26
    }
27
28
    /**
29
     * Creates and return a JSON Web Token through the OpenLRW API by using credentials filled in parameters.yml
30
     *
31
     * @return mixed|\Psr\Http\Message\ResponseInterface
32
     */
33
    protected function createJWT()
34
    {
35
        return json_decode( $this->http->request('POST', 'auth/login', [
36
            'headers' => [ 'X-Requested-With' => 'XMLHttpRequest' ],
37
            'json' => [
38
                'username' => env('API_USERNAME'),
39
                'password' => env('API_PASSWORD')
40
            ]
41
        ])->getBody()
42
          ->getContents());
43
    }
44
45
    /**
46
     * Stops the script and prints info about a variable
47
     *
48
     * @param mixed $variable
49
     */
50
    protected function debug($variable)
51
    {
52
        die('<pre>' . print_r($variable, true) . '</pre>');
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
53
    }
54
55
56
    /**
57
     * Gets a service from the container.
58
     *
59
     * @param string $service
60
     *
61
     * @return mixed
62
     */
63
    public function __get($service)
64
    {
65
        return $this->container->get($service);
66
    }
67
68
    /**
69
     * Executes a LDAP query
70
     *
71
     * @param $filter
72
     * @param array $arg
73
     * @return resource
74
     */
75
    protected function searchLDAP($filter, $arg = [])
76
    {
77
        return ldap_search($this->__get('ldap'), env('LDAP_BASE_DN'), $filter, $arg);
0 ignored issues
show
Bug introduced by
It seems like env('LDAP_BASE_DN') can also be of type array; however, parameter $base_dn of ldap_search() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
        return ldap_search($this->__get('ldap'), /** @scrutinizer ignore-type */ env('LDAP_BASE_DN'), $filter, $arg);
Loading history...
Bug introduced by
$this->__get('ldap') of type object is incompatible with the type resource expected by parameter $link_identifier of ldap_search(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

77
        return ldap_search(/** @scrutinizer ignore-type */ $this->__get('ldap'), env('LDAP_BASE_DN'), $filter, $arg);
Loading history...
78
    }
79
80
    /**
81
     * Returns data from a LDAP query
82
     *
83
     * @param $filter
84
     * @param array $arg
85
     * @return mixed
86
     */
87
    protected function ldap($filter, $arg = [])
88
    {
89
        return ldap_get_entries($this->__get('ldap'), $this->searchLDAP($filter, $arg));
0 ignored issues
show
Bug introduced by
$this->__get('ldap') of type object is incompatible with the type resource expected by parameter $link_identifier of ldap_get_entries(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

89
        return ldap_get_entries(/** @scrutinizer ignore-type */ $this->__get('ldap'), $this->searchLDAP($filter, $arg));
Loading history...
90
    }
91
92
    /**
93
     * Returns the first tuple from a LDAP query
94
     *
95
     * @param $filter
96
     * @param array $arg
97
     * @return mixed
98
     */
99
    protected function ldapFirst($filter, $arg = [])
100
    {
101
        return ldap_get_entries($this->__get('ldap'), $this->searchLDAP($filter, $arg))[0];
0 ignored issues
show
Bug introduced by
$this->__get('ldap') of type object is incompatible with the type resource expected by parameter $link_identifier of ldap_get_entries(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

101
        return ldap_get_entries(/** @scrutinizer ignore-type */ $this->__get('ldap'), $this->searchLDAP($filter, $arg))[0];
Loading history...
102
    }
103
104
105
}