Issues (158)

app/Console/Commands/SyncLoans.php (2 issues)

1
<?php
2
3
namespace App\Console\Commands;
4
5
use App\Loan;
6
use Illuminate\Console\Command;
0 ignored issues
show
This use statement conflicts with another class in this namespace, App\Console\Commands\Command. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
7
use Scriptotek\Alma\Client as AlmaClient;
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),
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)) {
0 ignored issues
show
The condition is_null($library->temporary_barcode) is always false.
Loading history...
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