SnapshotDetailsRepository::validate()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 20
rs 9.4285
cc 3
eloc 11
nc 4
nop 1
1
<?php namespace App\Repositories;
2
3
use App\Exceptions\RepositoryException;
4
use App\Models\SnapshotDetails;
5
use Session;
6
use Exception;
7
use Auth;
8
use Validator;
9
10
class SnapshotDetailsRepository extends Repository {
11
12
    public function getModelName()
13
    {
14
        return 'App\Models\SnapshotDetails';
15
    }
16
17 View Code Duplication
    public function get($id)
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...
18
    {
19
        $this->validateID($id);
20
21
        try {
22
            return $this->model->findOrFail($id);
23
        }
24
        catch(\Exception $e) {
25
            throw new RepositoryException('Could not retrieve snapshot detail', RepositoryException::DATABASE_ERROR);
26
        }
27
    }
28
29
	public function fromSnapshot($id)
30
	{
31
		$this->validateID($id);
32
33
		try {
34
			return $this->model->group( Session::get('groupID') )
35
									->leftJoin('users', 'snapshot_details.user_id', '=', 'users.user_id')
36
									->where('snapshot_details.cs_id', '=', $id)
37
									->get();
38
		}
39
		catch(Exception $e) {
40
			throw new RepositoryException('Database error', RepositoryException::DATABASE_ERROR);
41
		}
42
	}
43
44
    public function APIFormat($object)
45
	{
46
		if( !is_object($object) )
47
			return null;
48
49
		if( get_class($object) === 'SnapshotDetails' )
50
			return $this->formatRecord( $object );
51
52
		$responseArray = [];
53
		foreach ($object as $record) {
54
55
			array_push($responseArray, $this->formatRecord( $record ));
56
		}
57
58
		return $responseArray;
59
	}
60
61
    public function store(array $data)
62
    {
63
        $this->validate($data);
64
65
        try {
66
            $detail = new SnapshotDetails();
67
68
            $detail->type    = $data['type'];
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<App\Models\SnapshotDetails>. 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...
69
            $detail->sum     = floatval($data['sum']);
0 ignored issues
show
Documentation introduced by
The property sum does not exist on object<App\Models\SnapshotDetails>. 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...
70
            $detail->timed   = date('Y-m-d G:i:s', $data['time']);
0 ignored issues
show
Documentation introduced by
The property timed does not exist on object<App\Models\SnapshotDetails>. 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...
71
            $detail->user_id = Auth::id();
0 ignored issues
show
Documentation introduced by
The property user_id does not exist on object<App\Models\SnapshotDetails>. 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...
72
            $detail->cs_id   = $data['cs_id'];
0 ignored issues
show
Documentation introduced by
The property cs_id does not exist on object<App\Models\SnapshotDetails>. 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...
73
74
            if( $data['type']==='SALE' ) {
75
                $detail->sale_id = $data['sale_id'];
0 ignored issues
show
Documentation introduced by
The property sale_id does not exist on object<App\Models\SnapshotDetails>. 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...
76
            }
77
            if( isset($data['comment']) ) {
78
                $detail->comment = $data['comment'];
0 ignored issues
show
Documentation introduced by
The property comment does not exist on object<App\Models\SnapshotDetails>. 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...
79
            }
80
81
            $detail->save();
82
        }
83
        catch(\Exception $e) {
84
            throw new RepositoryException('Could not save snapshot detail in database', RepositoryException::DATABASE_ERROR);
85
        }
86
    }
87
88 View Code Duplication
    public function delete($id)
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...
89
    {
90
        $this->validateID($id);
91
92
        try {
93
            $this->model->destroy($id);
94
        }
95
        catch(\Exception $e) {
96
            throw new RepositoryException('Could not delete snapshot detail', RepositoryException::DATABASE_ERROR);
97
        }
98
    }
99
100
    public function validate(array $data)
101
    {
102
        // If it's a sale, it must be positive. Otherwise, it's a cash operation and it can't be 0
103
        $sumExtraValidation = ( $data['type']==='SALE' ) ? '|min:0' : '|not_in:0';
104
105
        $validator = Validator::make($data,
106
            [
107
                'type'    => 'required|in:SALE,CASH',
108
                'sum'     => 'required|numeric'.$sumExtraValidation,
109
                'time'    => 'required|integer|min:0',
110
                'sale_id' => 'sometimes|integer|min:0',
111
                'cs_id'   => 'required|integer|min:0',
112
                'comment' => ''
113
            ]
114
        );
115
116
        if( $validator->fails() ) {
117
            throw new RepositoryException('Cash snapshot detail validation failed', RepositoryException::VALIDATION_FAILED);
118
        }
119
    }
120
121
    private function formatRecord($object) {
122
123
		$formatted = new stdClass();
124
			
125
		$formatted->id          = intval($object->csd_id);
126
		$formatted->user        = intval($object->user_id);
127
		$formatted->sale        = intval($object->sale_id);
128
		$formatted->sum         = floatval($object->sum);
129
130
		$formatted->type        = $object->type;
131
		$formatted->comment     = $object->comment;
132
		$formatted->time        = $object->timed;
133
134
		return $formatted;
135
	}
136
137
}