GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 62f792...ec26c3 )
by Simon
02:13
created

EventStoreBranchSwap::handle()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 19
rs 9.4286
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
namespace SmoothPhp\LaravelAdapter\Console;
3
4
use Illuminate\Config\Repository;
5
use Illuminate\Console\Command;
6
7
/**
8
 * Class EventStoreBranchSwap
9
 * @package SmoothPhp\LaravelAdapter\Console
10
 * @author Simon Bennett <[email protected]>
11
 */
12
final class EventStoreBranchSwap extends Command
13
{
14
15
    /**
16
     * The name and signature of the console command.
17
     *
18
     * @var string
19
     */
20
    protected $signature = 'smoothphp:swapbranch';
21
22
    /**
23
     * The console command description.
24
     *
25
     * @var string
26
     */
27
    protected $description = 'Command description';
28
29
    /** @var Repository */
30
    private $config;
31
32
    /**
33
     * Create a new command instance.
34
     *
35
     * @param Repository $config
36
     */
37
    public function __construct(Repository $config)
38
    {
39
        parent::__construct();
40
        $this->config = $config;
41
    }
42
43
    /**
44
     * Execute the console command.
45
     *
46
     * @return mixed
47
     */
48
    public function handle()
49
    {
50
        // Get current branch name
51
52
        //swap the confection
53
        // fouse rebuild
54
55
        //save config
56
        $branch = $this->getGitBranch();
57
58
59
        $this->replaceEnvConfig($branch);
60
61
        if (!\Schema::connection($this->config->get('cqrses.eventstore_connection'))->hasTable($branch)) {
62
            $this->call('smoothphp:buildeventstore', ['--force' => true]);
63
        }
64
        $this->call('smoothphp:rebuild');
65
66
    }
67
68
    protected function getGitBranch()
69
    {
70
        $shellOutput = [];
71
        exec('git branch | ' . "grep ' * '", $shellOutput);
72
        foreach ($shellOutput as $line) {
0 ignored issues
show
Bug introduced by
The expression $shellOutput of type null|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
73
            if (strpos($line, '* ') !== false) {
74
                return trim(strtolower(str_replace(['* ', '/'], ['', '-'], $line)));
75
            }
76
        }
77
78
        return null;
79
    }
80
81
    /**
82
     * @param $branch
83
     */
84
    protected function replaceEnvConfig($branch)
85
    {
86
        $envFilePath = base_path() . '/.env';
87
88
        $rebuildFunction = function ($data) use ($branch) {
89
            if (stristr($data, 'DB_TABLE_EVENTSTORE')) {
90
                return "DB_TABLE_EVENTSTORE={$branch}\n";
91
            }
92
93
            return $data;
94
        };
95
96
        $contentArray = array_map($rebuildFunction,file($envFilePath));
97
98
        file_put_contents($envFilePath, implode('',$contentArray));
99
    }
100
}
101