MatchObserver   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 3
dl 0
loc 52
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B updating() 0 21 5
B updated() 0 21 5
1
<?php
2
3
namespace App\Observers;
4
5
use App\Events\MatchWasFinished;
6
use App\Events\Tournament\RoundHasBeenFinished;
7
use App\Match;
8
use App\Tournament;
9
10
/**
11
 * Class MatchObserver
12
 * @package App\Observers
13
 */
14
class MatchObserver
15
{
16
    /**
17
     * @param Match $model
18
     */
19
    public function updating(Match $model)
20
    {
21
        $dirtyStatus = array_get($model->getDirty(), 'status');
22
23
        // original score values
24
        $originalHomeScore = $model->getOriginal('homeScore');
25
        $originalAwayScore = $model->getOriginal('awayScore');
26
27
        // dirty score values
28
        $dirtyHomeScore = array_get($model->getDirty(), 'homeScore');
29
        $dirtyAwayScore = array_get($model->getDirty(), 'awayScore');
30
31
        $isMatchFinished = Match::RESULT_TYPE_UNKNOWN === $model->getOriginal('resultType')
32
            && Match::STATUS_FINISHED === $dirtyStatus;
33
34
        if ($isMatchFinished
35
            || ($originalHomeScore !== $dirtyHomeScore || $originalAwayScore !== $dirtyAwayScore)
36
        ) {
37
            event(new MatchWasFinished($model));
38
        }
39
    }
40
41
    /**
42
     * @param Match $model
43
     */
44
    public function updated(Match $model)
45
    {
46
        $status = array_get($model->getDirty(), 'status');
47
        $resultType = array_get($model->getDirty(), 'resultType');
48
49
        if (Match::RESULT_TYPE_UNKNOWN !== $resultType
50
            && Match::STATUS_FINISHED === $status
51
        ) {
52
            $currentRound = $model->round;
0 ignored issues
show
Documentation introduced by
The property round does not exist on object<App\Match>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
53
54
            $matches = $model->tournament->matches()
0 ignored issues
show
Documentation introduced by
The property tournament does not exist on object<App\Match>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
55
                ->where([
56
                    'round' => $currentRound, 'status' => Match::STATUS_NOT_STARTED
57
                ])
58
                ->get();
59
60
            if (Tournament::TYPE_KNOCK_OUT == $model->tournament->type && $matches->count() < 1) {
0 ignored issues
show
Documentation introduced by
The property tournament does not exist on object<App\Match>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
61
                event(new RoundHasBeenFinished($model->tournament));
0 ignored issues
show
Documentation introduced by
The property tournament does not exist on object<App\Match>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
62
            }
63
        }
64
    }
65
}
66