VendorRepository   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 4
dl 0
loc 75
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getModel() 0 4 1
A create() 0 22 2
A update() 0 20 2
A getPublic() 0 7 1
1
<?php
2
3
namespace App\Repositories;
4
5
use App\Orders\CreateWalletOrder;
6
use App\Services\ImageProcessor;
7
use App\Vendor;
8
use Auth;
9
10
class VendorRepository extends Repository
11
{
12
    /**
13
     * @return Vendor
14
     */
15
    public function getModel()
16
    {
17
        return new Vendor();
18
    }
19
20
    /**
21
     * Create method.
22
     *
23
     * @param array $data
24
     * @return Vendor
25
     */
26
    public function create(array $data)
27
    {
28
        $vendor = self::getModel()
29
            ->create([
30
                'user_id' => Auth::id(),
31
                'name' => $data['name'],
32
                'email' => $data['email'],
33
                'phone' => $data['phone'],
34
                'description' => $data['description']
35
            ]);
36
37
        (new CreateWalletOrder(\Auth::user()));
38
39
        if (isset($data['image'])) {
40
            $file = $data['image'];
41
            $location = 'upload/vendors/' . $vendor->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Vendor>. 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...
42
            $processor = new ImageProcessor();
43
            $processor->uploadAndCreate($file, $vendor, $data, $location);
44
        }
45
46
        return $vendor;
47
    }
48
49
    /**
50
     * Update method.
51
     *
52
     * @param Vendor $vendor
53
     * @param $data
54
     * @throws \Exception
55
     */
56
    public function update(Vendor $vendor, $data)
57
    {
58
        $vendor->fill([
59
            'name' => $data['name'],
60
            'email' => $data['email'],
61
            'description' => $data['description'],
62
            'phone' => $data['phone']
63
        ]);
64
65
        if (isset($data['image'])) {
66
            $file = $data['image'];
67
            $location = 'upload/vendors/' . $vendor->id;
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<App\Vendor>. 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...
68
            $processor = new ImageProcessor();
69
            $processor->destroy($vendor->images()->first()); // delete old logo.
70
71
            $processor->uploadAndCreate($file, $vendor, $data, $location);
72
        }
73
74
        $vendor->save();
75
    }
76
77
    public function getPublic($paginate = 9)
78
    {
79
        return $this->getModel()
0 ignored issues
show
Bug introduced by
The method active() does not exist on App\Vendor. Did you maybe mean scopeActive()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
80
            ->active() //todo: get popular first...
81
            ->orderBy('id', 'DESC')
82
            ->paginate($paginate);
83
    }
84
}