Passed
Push — master ( 3d161a...a93d1a )
by Dan Michael O.
03:19
created

SyncUsers::processUser()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 48
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 35
nc 10
nop 1
dl 0
loc 48
rs 8.7377
c 0
b 0
f 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\User;
6
use Illuminate\Console\Command;
0 ignored issues
show
Bug introduced by
The type Illuminate\Console\Command 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...
7
8
use PDOException;
9
use Scriptotek\Alma\Client as AlmaClient;
0 ignored issues
show
Bug introduced by
The type Scriptotek\Alma\Client 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...
10
11
class SyncUsers extends Command
12
{
13
    /**
14
     * The name and signature of the console command.
15
     *
16
     * @var string
17
     */
18
    protected $signature = 'bibrex:sync-users';
19
20
    /**
21
     * The console command description.
22
     *
23
     * @var string
24
     */
25
    protected $description = 'Sync all users';
26
27
    /**
28
     * Create a new command instance.
29
     *
30
     * @param AlmaClient $alma
31
     */
32
    public function __construct(AlmaClient $alma)
33
    {
34
        parent::__construct();
35
        $this->alma = $alma;
0 ignored issues
show
Bug Best Practice introduced by
The property alma does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
36
    }
37
38
    /**
39
     * Update a single user
40
     *
41
     * @param User $user
42
     */
43
    protected function processUser(User $user)
44
    {
45
        echo " - $user->name: ";
46
47
        if ($user->in_alma) {
48
            if ($user->updateFromAlma($this->alma)) {
49
                if ($user->isDirty()) {
50
                    \Log::info(sprintf(
51
                        'Oppdaterte brukeren <a href="%s">%s</a> fra Alma.',
52
                        action('UsersController@getShow', $user->id),
0 ignored issues
show
Bug introduced by
The function action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

52
                        /** @scrutinizer ignore-call */ 
53
                        action('UsersController@getShow', $user->id),
Loading history...
53
                        $user->name
54
                    ));
55
56
                    $this->info('oppdatert');
57
                } else {
58
                    $this->line('ingen endringer');
59
                }
60
            } else {
61
                \Log::info(sprintf(
62
                    'Brukeren <a href="%s">%s</a> ble ikke lenger funnet i Alma!',
63
                    action('UsersController@getShow', $user->id),
64
                    $user->name
65
                ));
66
67
                $this->warn('ikke i Alma lenger');
68
            }
69
        } else {
70
            if ($user->updateFromAlma($this->alma)) {
71
                \Log::info(sprintf(
72
                    'Lenket brukeren <a href="%s">%s</a> til en Alma-bruker.',
73
                    action('UsersController@getShow', $user->id),
74
                    $user->name
75
                ));
76
77
                $this->info('lenket til Alma-bruker');
78
            } else {
79
                $this->line('ikke i Alma');
80
            }
81
        }
82
        try {
83
            $user->save();
84
        } catch (PDOException $e) {
85
            $this->error('Konflikt!');
86
            \Log::info(sprintf(
87
                'Brukeren <a href="%s">%s</a> kunne ikke lagres på grunn av en konflikt - to brukere har ' .
88
                'samme strekkode eller feide-id. Sjekk i brukerlista om det er to brukere som kan slås sammen.',
89
                action('UsersController@getShow', $user->id),
90
                $user->name
91
            ));
92
        }
93
    }
94
95
    /**
96
     * Execute the console command.
97
     *
98
     * @return mixed
99
     */
100
    public function handle()
101
    {
102
        foreach (User::get() as $user) {
103
            $this->processUser($user);
104
        }
105
    }
106
}
107