Passed
Push — master ( af710d...2c0b17 )
by Dan Michael O.
08:29
created

SyncLoans   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 42
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 9

3 Methods

Rating   Name   Duplication   Size   Complexity  
A handle() 0 5 3
A __construct() 0 4 1
A processLoan() 0 57 5
1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Loan;
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
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...
8
9
class SyncLoans extends Command
10
{
11
    /**
12
     * The name and signature of the console command.
13
     *
14
     * @var string
15
     */
16
    protected $signature = 'bibrex:sync-loans';
17
18
    /**
19
     * The console command description.
20
     *
21
     * @var string
22
     */
23
    protected $description = 'Check in loans that have been returned in Alma';
24
25
    /** @var AlmaClient */
26
    protected $alma;
27
28
    /**
29
     * Create a new command instance.
30
     *
31
     * @return void
32
     */
33
    public function __construct(AlmaClient $alma)
34
    {
35
        parent::__construct();
36
        $this->alma = $alma;
37
    }
38
39
    /**
40
     * Execute the console command.
41
     *
42
     * @return mixed
43
     */
44
    public function handle()
45
    {
46
        foreach (Loan::with('item', 'item.thing')->get() as $loan) {
47
            if ($loan->item->thing_id == 1) {
48
                $this->processLoan($loan);
49
            }
50
        }
51
    }
52
53
    protected function processLoan(Loan $loan)
54
    {
55
        $barcode = $loan->item->barcode;
56
        $library = $loan->library;
57
58
        echo " - $barcode: ";
59
60
        $errBecause = sprintf(
61
            'Kunne ikke sjekke lån av <a href="%s">%s</a> mot Alma fordi',
62
            action('ItemsController@show', $loan->item_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

62
            /** @scrutinizer ignore-call */ 
63
            action('ItemsController@show', $loan->item_id),
Loading history...
63
            $barcode
64
        );
65
66
        $sucBecause = sprintf(
67
            'Fjerner lokalt lån av <a href="%s">%s</a> fordi',
68
            action('ItemsController@show', $loan->item_id),
69
            $barcode
70
        );
71
72
        if (is_null($library->temporary_barcode)) {
73
            $this->line('konfigfeil');
74
            \Log::error("$errBecause biblioteket ikke lenger har et midlertidig lånekort.");
75
            return;
76
        }
77
78
        $tempUser = $this->alma->users[$library->temporary_barcode];
79
80
        if (is_null($tempUser)) {
81
            $this->line('konfigfeil');
82
            \Log::error("$errBecause brukeren '{$library->temporary_barcode}' ikke ble funnet i Alma.");
83
            return;
84
        }
85
86
        $almaItem = $this->alma->items->fromBarcode($barcode);
87
        $almaLoan = $almaItem->loan;
88
89
        if (is_null($almaLoan)) {
90
            $this->line('returnert i Alma');
91
92
            // Delete local loan and item
93
            $loan->checkIn();
94
95
            \Log::info("$sucBecause dokumentet har blitt returnert i Alma.");
96
            return;
97
        }
98
99
        if ($almaLoan->user_id != $library->temporary_barcode) {
100
            $this->line('utlånt til annen person i Alma');
101
102
            // Delete local loan and item
103
            $loan->checkIn();
104
105
            \Log::info("$sucBecause dokumentet har blitt utlånt til en annen person i Alma.");
106
            return;
107
        }
108
109
        $this->line('fremdeles utlånt i Alma');
110
    }
111
}
112