RedirectService::findByUrlAndActive()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Hideyo\Ecommerce\Framework\Services\Redirect;
4
5
use Validator;
6
use File;
7
use Hideyo\Ecommerce\Framework\Services\Redirect\Entity\RedirectRepository;
8
use Hideyo\Ecommerce\Framework\Services\BaseService;
9
 
10
class RedirectService extends BaseService
11
{
12
	public function __construct(RedirectRepository $taxRate)
13
	{
14
		$this->repo = $taxRate;
0 ignored issues
show
Bug Best Practice introduced by
The property repo does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
15
	} 
16
17
    /**
18
     * The validation rules for the model.
19
     *
20
     * @param  integer  $redirectId id attribute model    
21
     * @return array
22
     */
23
    public function rules($redirectId = false)
24
    {
25
        $rules = array(
26
            'url' => 'required|unique_with:'.$this->repo->getModel()->getTable().', shop_id'
27
        );
28
        
29
        if ($redirectId) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $redirectId of type false|integer is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== false instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
30
            $rules['url'] = 'required|unique_with:'.$this->repo->getModel()->getTable().', shop_id, '.$redirectId.' = id';
31
        }
32
33
        return $rules;
34
    }
35
36
    public function create(array $attributes)
37
    {
38
        $attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id;
0 ignored issues
show
Bug introduced by
Accessing selected_shop_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
39
        $validator = Validator::make($attributes, $this->rules());
40
41
        if ($validator->fails()) {
42
            return $validator;
43
        }
44
45
        $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
46
        $this->repo->getModel()->fill($attributes);
47
        $this->repo->getModel()->save();
48
        return $this->repo->getModel();
49
    }
50
51
    public function updateById(array $attributes, $id)
52
    {
53
        $attributes['shop_id'] = auth('hideyobackend')->user()->selected_shop_id;
0 ignored issues
show
Bug introduced by
Accessing selected_shop_id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
54
        $validator = Validator::make($attributes, $this->rules($id));
55
        if ($validator->fails()) {
56
            return $validator;
57
        }
58
59
        $model = $this->find($id);
60
        $attributes['modified_by_user_id'] = auth('hideyobackend')->user()->id;
0 ignored issues
show
Bug introduced by
Accessing id on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
61
62
        if (count($attributes) > 0) {
63
            $model->fill($attributes);
64
            $model->save();
65
        }
66
        return $model;  
67
    } 
68
69
    public function importCsv($results, $shopId)
70
    {
71
        foreach ($results as $row) {
72
73
            $attributes = $row->toArray();
74
            $attributes['shop_id'] = $shopId;
75
            $attributes['active'] = 0;
76
     
77
            $validator = Validator::make($attributes, $this->rules());
78
79
            if ($validator->fails()) {
80
    
81
                $result = $this->repo->getModel()->where('url', '=', $attributes['url'])->get()->first();
82
                if ($result) {
83
                    $attributes['active'] = 0;
84
                    if($attributes['redirect_url']) {
85
                        $attributes['active'] = 1;
86
                    } 
87
                    $model = $this->find($result->id);
88
		            $model->fill($attributes);
89
		            $model->save();
90
91
		            return $model;
92
                }
93
94
            } else {
95
                $redirect = new Redirect;
96
                $redirect->fill($attributes);
97
                $redirect->save();
98
         
99
            }
100
        }
101
102
        return true;
103
    }	
104
105
    public function updateClicks($url)
106
    {
107
        $result = $this->repo->findByUrl($url);
108
        if ($result) {
109
            $model = $this->find($result->id);
110
            $model->fill(array('clicks' => $result->clicks + 1));
111
            $model->save();
112
113
            return $model;
114
115
        }
116
    }
117
118
    public function findByUrl($url)
119
    {
120
        return $this->repo->findByUrl($url);
121
    }
122
123
124
    public function findByUrlAndActive($url)
125
    {
126
        return $this->repo->findByUrlAndActive($url);
127
    }
128
129
130
}