SaleRepository::rankUsersByInterval()   B
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 27
rs 8.8571
cc 3
eloc 16
nc 4
nop 1
1
<?php namespace App\Repositories;
2
3
use Carbon\Carbon;
4
use Carbon\CarbonInterval;
5
use App\Exceptions\RepositoryException;
6
use App\Models\Sales;
7
use Auth;
8
use DB;
9
use Exception;
10
use Session;
11
use Validator;
12
13
class SaleRepository extends Repository
14
{
15
    public function getModelName()
16
    {
17
        return 'App\Models\Sales';
18
    }
19
20
    public function register(array $saleData)
21
    {
22
        $this->validate($saleData);
23
24
        try {
25
            $sale = new Sales();
26
27
            $sale->group_id = Session::get('groupID');
0 ignored issues
show
Documentation introduced by
The property group_id does not exist on object<App\Models\Sales>. 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...
28
            $sale->user_id = Auth::id();
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<App\Models\Sales>. 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...
29
            $sale->time = date('Y-m-d G:i:s', $saleData['time']);
0 ignored issues
show
Documentation introduced by
The property time does not exist on object<App\Models\Sales>. 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...
30
            $sale->sum = floatval($saleData['sum']);
0 ignored issues
show
Documentation introduced by
The property sum does not exist on object<App\Models\Sales>. 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...
31
            $sale->paid = floatval($saleData['paid']);
0 ignored issues
show
Documentation introduced by
The property paid does not exist on object<App\Models\Sales>. 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...
32
            $sale->is_active = true;
0 ignored issues
show
Documentation introduced by
The property is_active does not exist on object<App\Models\Sales>. 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...
33
34
            $sale->save();
35
36
            return DB::getPdo()->lastInsertId();
37
        }
38
        catch (Exception $e)
39
        {
40
            throw new RepositoryException('Could not save sale in database: ' . $e->getMessage(), RepositoryException::DATABASE_ERROR);
41
        }
42
    }
43
44
    public function countByInterval(CarbonInterval $interval)
45
    {
46
        $beginDate = Carbon::now()->sub($interval)->addHour();
47
48
        if( $interval->d>0 )
49
        {
50
            $beginDate = $beginDate->startOfDay();
51
        }
52
53
        $intervals = [];
54
        $dateCounter = clone $beginDate;
55
        while($dateCounter <= Carbon::now())
56
        {
57
            if( $interval->h>0 )
58
            {
59
                $intervals[$dateCounter->hour] = 0;
60
                $dateCounter->addHour(1);
61
            }
62
            else
63
            {
64
                $intervals[$dateCounter->day] = 0;
65
                $dateCounter->addDay(1);
66
            }
67
        }
68
69
        try
70
        {
71
            $sales = $this->model->where('is_active', '=', true)
72
                                ->where('time', '>=', $beginDate)
73
                                ->get();
74
        }
75
        catch(Exception $e)
76
        {
77
            throw new RepositoryException('Could not get sale data', RepositoryException::DATABASE_ERROR);
78
        }
79
80
        foreach($sales as $sale)
81
        {
82
            $carbonTimestamp = Carbon::createFromFormat('Y-m-d H:m:s', $sale->time);
83
84
            if( $interval->h>0 )
85
            {
86
                $intervals[$carbonTimestamp->hour]++;
87
            }
88
            else
89
            {
90
                $intervals[$carbonTimestamp->day]++;
91
            }
92
        }
93
94
        return $intervals;
95
    }
96
97
    public function rankUsersByInterval(CarbonInterval $interval)
98
    {
99
        $beginDate = Carbon::now()->sub($interval)->addHour();
100
101
        if( $interval->d>0 )
102
        {
103
            $beginDate = $beginDate->startOfDay();
104
        }
105
106
        try
107
        {
108
            $userRank = $this->model->select(['firstname', 'lastname', DB::raw('COUNT(*) as count')])
109
                ->join('users', 'sales.user_id', '=', 'users.user_id')
110
                ->where('sales.is_active', '=', true)
111
                ->where('sales.time', '>=', $beginDate)
112
                ->groupBy('sales.user_id')
113
                ->orderBy('count', 'DESC')
114
                ->take(10)
115
                ->get();
116
        }
117
        catch(Exception $e)
118
        {
119
            throw new RepositoryException('Could not retrieve users', RepositoryException::DATABASE_ERROR);
120
        }
121
122
        return $userRank;
123
    }
124
125 View Code Duplication
    public function validate(array $data)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
126
    {
127
        $validator = Validator::make($data,
128
            [
129
                'time' => 'required|integer|min:0',
130
                'sum' => 'required|numeric|min:0',
131
                'paid' => 'required|numeric|min:0'
132
            ]
133
        );
134
135
        if ($validator->fails()) {
136
            throw new RepositoryException('Sale data validation failed', RepositoryException::VALIDATION_FAILED);
137
        }
138
    }
139
140
}
141