GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — rewrite-laravel ( 75413a...5c1bf5 )
by Oliver
05:22
created

MmexService   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 129
Duplicated Lines 21.71 %

Coupling/Cohesion

Components 0
Dependencies 6

Importance

Changes 4
Bugs 2 Features 0
Metric Value
wmc 17
lcom 0
cbo 6
dl 28
loc 129
rs 10
c 4
b 2
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A getTransactions() 0 23 1
A deleteAccounts() 0 5 1
A importBankAccounts() 0 9 2
A deletePayees() 0 5 1
B importPayees() 0 24 3
A deleteCategories() 0 5 1
A importCategories() 0 14 3
A createOrGetCategory() 14 14 2
A createOrGetSubCategory() 14 14 2
A deleteTransactions() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: okaufmann
5
 * Date: 22.10.2016
6
 * Time: 14:53.
7
 */
8
9
namespace App\Services;
10
11
use App\Models\Account;
12
use App\Models\Category;
13
use App\Models\Payee;
14
use App\Models\Transaction;
15
use Log;
16
17
class MmexService
18
{
19
    public function getTransactions()
20
    {
21
        $transactions = Transaction::all();
22
23
        return $transactions;
24
        // example
0 ignored issues
show
Unused Code Comprehensibility introduced by
49% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
25
//        return [
26
//            0 => [
27
//                'ID'          => 1,
28
//                'Date'        => '2016-10-07',
29
//                'Account'     => 'Another Account',
30
//                'ToAccount'   => 'None',
31
//                'Status'      => 'R',
32
//                'Type'        => 'Zahlung',
33
//                'Payee'       => 'Migros',
34
//                'Category'    => 'Einkauf',
35
//                'SubCategory' => 'Etwas anderes',
36
//                'Amount'      => '123',
37
//                'Notes'       => "Das ist ein \r\nMeeeeehrzeiliger \r\nText",
38
//                'Attachments' => 'Transaction_1_Attach1.png;Transaction_1_Attach2.jpg',
39
//            ],
40
//        ];
41
    }
42
43
    public function deleteAccounts()
44
    {
45
        // TODO: find better method than where hack
46
        Account::where('id', '>', 0)->delete();
47
    }
48
49
    public function importBankAccounts($postData)
50
    {
51
        Log::debug('MmexController.importBankAccounts(), $accounts', [$postData->Accounts]);
52
        foreach ($postData->Accounts as $account) {
53
            Account::create([
54
                'name' => $account->AccountName,
55
            ]);
56
        }
57
    }
58
59
    public function deletePayees()
60
    {
61
        // TODO: find better method than where hack
62
        Payee::where('id', '>', 0)->delete();
63
    }
64
65
    public function importPayees($postData)
66
    {
67
        Log::debug('MmexController.importPayees(), $payees', [$postData->Payees]);
68
69
        foreach ($postData->Payees as $payee) {
70
            $categoryName = $payee->DefCateg;
71
            $subCategoryName = $payee->DefSubCateg;
72
73
            $category = Category::where('name', $subCategoryName)->whereHas('parentCategory', function ($query) use ($categoryName) {
74
                $query->where('name', $categoryName);
75
            })->first();
76
77
            if ($category) {
78
                $category->defaultForPayees()->create([
79
                    'name' => $payee->PayeeName,
80
                ]);
81
            } else {
82
                // ignore default/last category and just create entry
83
                Payee::create([
84
                    'name' => $payee->PayeeName,
85
                ]);
86
            }
87
        }
88
    }
89
90
    public function deleteCategories()
91
    {
92
        // TODO: find better method than where hack
93
        Category::where('id', '>', 0)->delete();
94
    }
95
96
    public function importCategories($postData)
97
    {
98
        $categories = collect($postData->Categories);
99
100
        $grouped = $categories->groupBy('CategoryName');
101
102
        foreach ($grouped as $categoryName => $subCategories) {
103
            $category = $this->createOrGetCategory($categoryName);
104
105
            foreach ($subCategories as $subCategory) {
106
                $this->createOrGetSubCategory($category, $subCategory->SubCategoryName);
107
            }
108
        }
109
    }
110
111 View Code Duplication
    private function createOrGetCategory($name)
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...
112
    {
113
        $existingCategory = Category::whereName($name)->first();
114
115
        if ($existingCategory) {
116
            return $existingCategory;
117
        }
118
119
        $newCategory = Category::create([
120
            'name' => $name,
121
        ]);
122
123
        return $newCategory;
124
    }
125
126 View Code Duplication
    private function createOrGetSubCategory(Category $parentCategory, $name)
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...
127
    {
128
        $existingCategory = Category::whereName($name)->first();
129
130
        if ($existingCategory) {
131
            return $existingCategory;
132
        }
133
134
        $newCategory = $parentCategory->subCategories()->create([
135
            'name' => $name,
136
        ]);
137
138
        return $newCategory;
139
    }
140
141
    public function deleteTransactions($transactionId)
142
    {
143
        Transaction::whereId($transactionId)->delete();
144
    }
145
}
146