Completed
Pull Request — master (#7)
by
unknown
02:36
created

DumbPasswordServiceProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 6
c 3
b 0
f 1
lcom 1
cbo 0
dl 0
loc 70
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 0 9 1
A register() 0 4 1
A provides() 0 4 1
A generateDictionary() 0 15 3
1
<?php
2
3
/*
4
 * This file is part of the Laravel Password package.
5
 *
6
 * (c) Prosper Otemuyiwa <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Unicodeveloper\DumbPassword;
13
14
use Illuminate\Support\ServiceProvider;
15
use Validator;
16
17
class DumbPasswordServiceProvider extends ServiceProvider
18
{
19
20
    /*
21
    * Indicates if loading of the provider is deferred.
22
    *
23
    * @var bool
24
    */
25
    protected $defer = false;
26
27
    /**
28
     * default error message
29
     *
30
     * @var string
31
     */
32
    protected $message = 'This password is just too common. Please try another!';
33
34
    /**
35
    * Publishes all the config file this package needs to function
36
    */
37
    public function boot()
38
    {
39
        $path = realpath(__DIR__.'/../resources/config/passwordlist.txt');
40
        $data = $this->generateDictionary($path);
41
42
       Validator::extend('dumbpwd', function($attribute, $value, $parameters, $validator) use ($data) {
0 ignored issues
show
Unused Code introduced by
The parameter $parameters is not used and could be removed.

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

Loading history...
Unused Code introduced by
The parameter $validator is not used and could be removed.

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

Loading history...
43
            return !array_key_exists($value, $data);
44
       }, $this->message);
45
    }
46
47
    /**
48
    * Register the application services.
49
    */
50
    public function register()
51
    {
52
53
    }
54
55
    /**
56
    * Get the services provided by the provider
57
    * @return array
58
    */
59
    public function provides()
60
    {
61
        return ['laravel-password'];
62
    }
63
64
    /**
65
     * Generates a dictionary from the file referenced by the path specified
66
     *
67
     * @param $path
68
     *
69
     * @return array
70
     */
71
    private function generateDictionary($path)
72
    {
73
        $dictionary = [];
74
75
        $handle = fopen($path, "r");
76
        if ($handle) {
77
            while (($line = fgets($handle)) !== false) {
78
                $dictionary[trim($line)] = true;
79
            }
80
81
            fclose($handle);
82
        }
83
84
        return $dictionary;
85
    }
86
}
87