PromotionRepository::store()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
namespace Gamer\Repositories;
4
5
use Siravel\Models\Negocios\Promotion;
6
use App\Repositories\CmsRepository;
7
use App\Repositories\TranslationRepository;
8
9
class PromotionRepository extends CmsRepository
10
{
11
    public $model;
12
13
    public $translationRepo;
14
15
    public $table;
16
17
    public function __construct(Promotion $model, TranslationRepository $translationRepo)
18
    {
19
        $this->model = $model;
20
        $this->translationRepo = $translationRepo;
21
        $this->table = \Illuminate\Support\Facades\Config::get('cms.db-prefix').'promotions';
22
    }
23
24
    /**
25
     * Stores Promotions into database.
26
     *
27
     * @param array $payload
28
     *
29
     * @return Promotions
30
     */
31
    public function store($payload)
32
    {
33
        $payload['slug'] = str_slug($payload['slug']);
34
35
        return $this->model->create($payload);
36
    }
37
38
    /**
39
     * Updates Promotion in the database
40
     *
41
     * @param Promotions $widget
42
     * @param array      $payload
43
     *
44
     * @return Promotions
45
     */
46
    public function update($widget, $payload)
47
    {
48
        $payload['slug'] = str_slug($payload['slug']);
49
50
        if (!empty($payload['lang']) && $payload['lang'] !== \Illuminate\Support\Facades\Config::get('cms.default-language', 'en')) {
51
            return $this->translationRepo->createOrUpdate($widget->id, 'Siravel\Models\Negocios\Promotion', $payload['lang'], $payload);
52
        } else {
53
            unset($payload['lang']);
54
55
            return $widget->update($payload);
56
        }
57
    }
58
}
59