Completed
Push — develop ( 9f1019...8cff1c )
by Greg
01:53
created

AbstractExportCommand::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Console\Commands;
4
5
use Illuminate\Console\Command;
6
use Illuminate\Support\Facades\Storage;
7
use App\Account;
8
9
abstract class AbstractExportCommand extends Command
10
{
11
12
    /**
13
     * The name and signature of the console command.
14
     *
15
     * @var string
16
     */
17
    protected $signature = '';
18
19
    /**
20
     * The console command description.
21
     *
22
     * @var string
23
     */
24
    protected $description = '';
25
26
    /**
27
     * The export folder location.
28
     *
29
     * @var string
30
     */
31
    protected $exportFolder = '';
32
33
    /**
34
     * The export file name.
35
     *
36
     * @var string
37
     */
38
    protected $exportFileName = '';
39
40
41
    /**
42
     * The export extension file.
43
     *
44
     * @var string
45
     */
46
    protected $exportFileExt = '.txt';
47
48
    /**
49
     * The export storage disk name.
50
     *
51
     * @var string
52
     */
53
    protected $storageDisk = 'export';
54
55
    /**
56
     * The export storage location.
57
     *
58
     * @var Illuminate\Contracts\Filesystem\Filesystem
59
     */
60
    protected $storage;
61
62
    /**
63
     * Create a new command instance.
64
     *
65
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
66
     */
67
    public function __construct()
68
    {
69
        parent::__construct();
70
        $this->storage = Storage::disk($this->storageDisk);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Illuminate\Support\Faca...isk($this->storageDisk) of type object<Illuminate\Contra...\Filesystem\Filesystem> is incompatible with the declared type object<App\Console\Comma...\Filesystem\Filesystem> of property $storage.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
71
    }
72
73
    /**
74
     * Execute the console command.
75
     *
76
     * @return mixed
77
     */
78
    abstract public function handle();
79
80
    final protected function exportAccounts($accounts, $filename, $password = false, $flagCI = false)
81
    {
82
        $this->storage->put($filename, '');
83
84
        $count = count($accounts);
85
86
        if ($flagCI === false) {
87
          $bar = $this->output->createProgressBar($count);
88
        }
89
90
        foreach ($accounts as $account) {
91
            $record = "{$account->netlogin}";
92
            $record .= $password ? ":{$account->netpass}" : '';
93
            $this->storage->prepend($filename, $record);
94
            if (isset($bar)) {
95
              $bar->advance();
96
            }
97
        }
98
99
        if (isset($bar)) {
100
          $bar->finish();
101
          $this->info("\n");
102
        }
103
104
        return $count;
105
    }
106
107
    final protected function fecthAccounts($filter=null)
108
    {
109
        $query = Account::where('status', Account::ACCOUNT_ENABLE);
110
111
        if(is_null($filter) === false && is_array($filter)) {
112
          $query = $query->where($filter);
113
        }
114
115
        return $query->orderBy('netlogin', 'desc')->get();
116
    }
117
}
118