Completed
Pull Request — master (#99)
by ARCANEDEV
09:12 queued 07:59
created

NoCaptchaManager   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 87
ccs 15
cts 15
cp 1
c 0
b 0
f 0
rs 10
wmc 7
lcom 1
cbo 3

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getDefaultDriver() 0 4 1
A version() 0 4 1
A createV2Driver() 0 4 1
A createV3Driver() 0 4 1
A buildDriver() 0 8 2
A config() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\NoCaptcha;
6
7
use Illuminate\Support\Manager;
8
use Arcanedev\NoCaptcha\Contracts\NoCaptcha as NoCaptchaContract;
9
use Arcanedev\NoCaptcha\Contracts\NoCaptchaManager as NoCaptchaManagerContract;
10
11
/**
12
 * Class     NoCaptchaManager
13
 *
14
 * @author   ARCANEDEV <[email protected]>
15
 */
16
class NoCaptchaManager extends Manager implements NoCaptchaManagerContract
17
{
18
    /* -----------------------------------------------------------------
19
     |  Getters
20
     | -----------------------------------------------------------------
21
     */
22
23
    /**
24
     * Get the default driver name.
25
     *
26
     * @return string
27
     */
28 4
    public function getDefaultDriver(): string
29
    {
30 4
        return $this->config('version');
31
    }
32
33
    /* -----------------------------------------------------------------
34
     |  Main Methods
35
     | -----------------------------------------------------------------
36
     */
37
38
    /**
39
     * Get the NoCaptcha driver by the given version.
40
     *
41
     * @param  string|null  $version
42
     *
43
     * @return \Arcanedev\NoCaptcha\NoCaptchaV3|\Arcanedev\NoCaptcha\NoCaptchaV2
44
     */
45 32
    public function version($version = null): NoCaptchaContract
46
    {
47 32
        return $this->driver($version);
48
    }
49
50
    /**
51
     * Create the v2 captcha.
52
     *
53
     * @return \Arcanedev\NoCaptcha\NoCaptchaV2
54
     */
55 2
    public function createV2Driver(): NoCaptchaContract
56
    {
57 2
        return $this->buildDriver(NoCaptchaV2::class);
58
    }
59
60
    /**
61
     * Create the v3 captcha.
62
     *
63
     * @return \Arcanedev\NoCaptcha\NoCaptchaV3
64
     */
65 14
    public function createV3Driver(): NoCaptchaContract
66
    {
67 14
        return $this->buildDriver(NoCaptchaV3::class);
68
    }
69
70
    /* -----------------------------------------------------------------
71
     |  Other Methods
72
     | -----------------------------------------------------------------
73
     */
74
75
    /**
76
     * Build a driver.
77
     *
78
     * @param  string  $driver
79
     *
80
     * @return \Arcanedev\NoCaptcha\Contracts\NoCaptcha
81
     */
82 28
    protected function buildDriver(string $driver): NoCaptchaContract
83
    {
84 28
        return $this->container->make($driver, [
85 28
            'secret'  => $this->config('secret'),
86 28
            'siteKey' => $this->config('sitekey'),
87 28
            'locale'  => $this->config('lang') ?: $this->container->getLocale(),
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Illuminate\Contracts\Container\Container as the method getLocale() does only exist in the following implementations of said interface: Illuminate\Foundation\Application.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
88
        ]);
89
    }
90
91
    /**
92
     * Get a value from the config file.
93
     *
94
     * @param  string      $key
95
     *
96
     * @return mixed
97
     */
98 28
    protected function config(string $key = '')
99
    {
100 28
        return $this->config->get("no-captcha.{$key}");
101
    }
102
}
103