Transaction   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 65
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A pointable() 0 4 1
A getCurrentPoints() 0 14 2
A addTransaction() 0 16 2
1
<?php
2
3
namespace Gamer\Models;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class Transaction extends Model
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $table = 'point_transactions';
13
14
    /**
15
     * @var array
16
     */
17
    protected $guarded = ['id', 'created_at', 'updated_at'];
18
19
    /**
20
     * @return \Illuminate\Database\Eloquent\Relations\MorphTo
21
     */
22
    public function pointable()
23
    {
24
        return $this->morphTo();
25
    }
26
27
    /**
28
     * @param Model $pointable
29
     *
30
     * @return static
31
     */
32
     public function getCurrentPoints(Model $pointable)
33
     {
34
         $currentPoint = Transaction::
35
         where('pointable_id', $pointable->id)
36
         ->where('pointable_type', $pointable->getMorphClass())
37
         ->orderBy('created_at', 'desc')
38
         ->pluck('current')->first();
39
40
         if (!$currentPoint) {
41
           $currentPoint = 0.0;
42
         }
43
44
         return $currentPoint;
45
     }
46
47
    /**
48
     * @param Model $pointable
49
     * @param $amount
50
     * @param $message
51
     * @param $data
52
     *
53
     * @return static
54
     */
55
    public function addTransaction(Model $pointable, $amount, $message, $data = null)
56
    {
57
        $transaction = new static();
58
        $transaction->amount = $amount;
0 ignored issues
show
Documentation introduced by
The property amount does not exist on object<Gamer\Models\Transaction>. 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...
59
60
        $transaction->current = $this->getCurrentPoints($pointable) + $amount;
0 ignored issues
show
Documentation introduced by
The property current does not exist on object<Gamer\Models\Transaction>. 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...
61
62
        $transaction->message = $message;
0 ignored issues
show
Documentation introduced by
The property message does not exist on object<Gamer\Models\Transaction>. 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...
63
        if ($data) {
64
          $transaction->fill($data);
65
        }
66
        // $transaction->save();
67
        $pointable->transactions()->save($transaction);
68
69
        return $transaction;
70
    }
71
}
72