Passed
Push — stage ( ff5096...3fe5d0 )
by Jon
07:30
created

Publish   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
dl 0
loc 83
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B handle() 0 49 5
1
<?php
2
3
namespace App\Jobs;
4
5
use App\Exceptions\GithubAuthenticationException;
6
use App\Models\Release;
7
use App\Notifications\Frontend\ReleaseWasPublished;
8
use App\Services\Publish\Git;
9
use App\Services\Publish\GitHubService;
10
use Illuminate\Bus\Queueable;
11
use Illuminate\Contracts\Queue\ShouldQueue;
12
use Illuminate\Foundation\Bus\Dispatchable;
13
use Illuminate\Queue\InteractsWithQueue;
14
use Illuminate\Queue\SerializesModels;
15
use Illuminate\Support\Carbon;
16
use Illuminate\Support\Facades\Redirect;
17
18
class Publish implements ShouldQueue
19
{
20
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
0 ignored issues
show
introduced by
The trait Illuminate\Queue\SerializesModels requires some properties which are not provided by App\Jobs\Publish: $id, $class
Loading history...
21
    /**
22
     * @var Release
23
     */
24
    private $release;
25
    /**
26
     * @var string
27
     */
28
    private $disk;
29
30
    /**
31
     * Create a new job instance.
32
     *
33
     * @param Release $release
34
     * @param string  $disk
35
     */
36
    public function __construct(Release $release, $disk = GenerateRdf::REPO_ROOT)
37
    {
38
        $this->release = $release;
39
        $this->disk    = $disk;
40
    }
41
42
    /**
43
     * Execute the job.
44
     *
45
     * @return void
46
     * @throws \InvalidArgumentException
47
     * @throws \GitWrapper\GitException
48
     * @throws \Symfony\Component\Process\Exception\RuntimeException
49
     * @throws \Symfony\Component\Process\Exception\ProcessFailedException
50
     * @throws \Symfony\Component\Process\Exception\LogicException
51
     */
52
    public function handle()
53
    {
54
        //todo:lot's more try/catch here
55
        $project = $this->release->project;
56
        $repo    = $project->repo;
0 ignored issues
show
Unused Code introduced by
The assignment to $repo is dead and can be removed.
Loading history...
57
        $user    = $this->release->user;
58
59
        //todo: rdf generator shouldn't responsible for storage management
60
        Git::initDir($project, $this->disk, $user);
0 ignored issues
show
Bug introduced by
It seems like $project can also be of type null; however, parameter $project of App\Services\Publish\Git::initDir() does only seem to accept App\Models\Project, maybe add an additional type check? ( Ignorable by Annotation )

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

60
        Git::initDir(/** @scrutinizer ignore-type */ $project, $this->disk, $user);
Loading history...
Bug introduced by
It seems like $user can also be of type null; however, parameter $user of App\Services\Publish\Git::initDir() does only seem to accept App\Models\Access\User\User, maybe add an additional type check? ( Ignorable by Annotation )

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

60
        Git::initDir($project, $this->disk, /** @scrutinizer ignore-type */ $user);
Loading history...
61
62
        //todo: this section should be in a transaction
63
        $this->release->published_at = Carbon::now();
64
        $this->release->save();
65
        //foreach selected vocabulary
66
        $vocabs = $this->release->vocabularies()->get();
67
        foreach ($vocabs as $vocab) {
68
            $job = new GenerateRdf($vocab, $this->release, $this->disk);
69
            $job->handle();
70
        }
71
        $vocabs = $this->release->elementsets()->get();
72
        foreach ($vocabs as $vocab) {
73
            $job = new GenerateRdf($vocab, $this->release, $this->disk);
74
            $job->handle();
75
        }
76
        //when the jobs are complete:
77
        //commit the generated rdf with the version as the commit message
78
        Git::commitDir($project, $this->release->name, $this->disk);
79
80
        if ($project->repo) {
81
            //push the repo to github
82
            Git::updateRemote($this->release, $this->disk, $user);
83
            //push the release to GitHub
84
            $gitHub = new GitHubService($this->release);
85
            try {
86
                $gitHub->setRelease();
87
            }
88
            catch (GithubAuthenticationException $e) {
89
                Redirect::back()->withErrors([
90
                    'You\'re trying to access GitHub, but you don\'t seem to have any current GitHub credentials.',
91
                    'You must logout of the OMR, and login to the OMR again using the \'Login with GitHub\' link.',
92
                ]);
93
            }
94
        } else {
95
            //tag the commit with the version
96
            Git::tagDir($project, $this->release->tag_name, $this->disk);
97
        }
98
        //todo: run the GenerateDocs job
99
        //notify the user that it's done
100
        $this->release->User->notify(new ReleaseWasPublished($this->release));
0 ignored issues
show
Bug introduced by
The property User does not seem to exist on App\Models\Release. Are you sure there is no database migration missing?

Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.

Loading history...
101
    }
102
}
103