Completed
Push — master ( a624e1...736705 )
by Nassif
12:28
created

Spreadsheet   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 19
lcom 2
cbo 5
dl 0
loc 141
ccs 54
cts 54
cp 1
rs 10
c 0
b 0
f 0

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A setLocales() 0 6 1
A setTranslations() 0 6 1
A getTranslations() 0 4 1
A getTranslationsCount() 0 4 1
A getId() 0 4 1
A getUrl() 0 4 1
A getLocales() 0 4 1
A getLocalesCount() 0 4 1
A getHeader() 0 7 1
A getCamelizedHeader() 0 10 2
A getHeaderColumnsCount() 0 6 1
A getHeaderRowsCount() 0 4 1
A translationsEmptySheetCoordinates() 0 8 1
A translationsSheetCoordinates() 0 9 1
A sheetStyles() 0 4 1
A api() 0 4 1
1
<?php
2
3
namespace Nikaia\TranslationSheet;
4
5
use Illuminate\Support\Str;
6
use Nikaia\TranslationSheet\Client\Api;
7
use Nikaia\TranslationSheet\Sheet\Styles;
8
use Nikaia\TranslationSheet\Sheet\TranslationsSheetCoordinates;
9
10
class Spreadsheet
11
{
12
    /** @var string */
13
    protected $id;
14
15
    /** @var array */
16
    protected $locales = [];
17
18
    /** @var array */
19
    protected $translations = [];
20
21
    /** @var Api */
22
    protected $api;
23
24 18
    public function __construct($id, $locales, $api = null)
25
    {
26 18
        $this->id = $id;
27 18
        $this->locales = $locales;
28 18
        $this->api = $api ?: app(Api::class);
29 18
    }
30
31 3
    public function setLocales($locales)
32
    {
33 3
        $this->locales = $locales;
34
35 3
        return $this;
36
    }
37
38 17
    public function setTranslations($translations)
39
    {
40 17
        $this->translations = $translations;
41
42 17
        return $this;
43
    }
44
45 1
    public function getTranslations()
46
    {
47 1
        return $this->translations;
48
    }
49
50 3
    public function getTranslationsCount()
51
    {
52 3
        return count($this->translations);
53
    }
54
55 3
    public function getId()
56
    {
57 3
        return $this->id;
58
    }
59
60 1
    public function getUrl()
61
    {
62 1
        return 'https://docs.google.com/spreadsheets/d/'.$this->id;
63
    }
64
65 10
    public function getLocales()
66
    {
67 10
        return $this->locales;
68
    }
69
70 4
    public function getLocalesCount()
71
    {
72 4
        return count($this->locales);
73
    }
74
75 7
    public function getHeader()
76
    {
77 7
        return array_merge(
78 7
            array_merge(['Full key'], $this->getLocales()),
79 7
            ['Namespace', 'Group', 'Key', 'Source file']
80
        );
81
    }
82
83 2
    public function getCamelizedHeader()
84
    {
85
        return array_map(function ($item) {
86 2
            if (in_array($item, Util::asArray($this->getLocales()))) {
87 2
                return $item;
88
            }
89
90 2
            return Str::camel($item);
91 2
        }, $this->getHeader());
92
    }
93
94 4
    public function getHeaderColumnsCount()
95
    {
96 4
        $header = $this->getHeader();
97
98 4
        return count($header);
99
    }
100
101 3
    public function getHeaderRowsCount()
102
    {
103 3
        return 1;
104
    }
105
106
    /**
107
     * @param $sheetId
108
     * @param $sheetTitle
109
     *
110
     * @return TranslationsSheetCoordinates
111
     */
112 1
    public function translationsEmptySheetCoordinates($sheetId, $sheetTitle)
113
    {
114 1
        return TranslationsSheetCoordinates::emptySheet(
115 1
            $this->getHeaderColumnsCount(),
116 1
            $this->getLocalesCount(),
117 1
            $this->getHeaderColumnsCount()
118 1
        )->setSheetId($sheetId)->setSheetTitle($sheetTitle);
119
    }
120
121
    /**
122
     * @param $sheetId
123
     * @param $sheetTitle
124
     * @return TranslationsSheetCoordinates
125
     */
126 2
    public function translationsSheetCoordinates($sheetId, $sheetTitle)
127
    {
128 2
        return TranslationsSheetCoordinates::sheetWithData(
129 2
            $this->getTranslationsCount(),
130 2
            $this->getHeaderColumnsCount(),
131 2
            $this->getLocalesCount(),
132 2
            $this->getHeaderRowsCount()
133 2
        )->setSheetId($sheetId)->setSheetTitle($sheetTitle);
134
    }
135
136 1
    public function sheetStyles()
137
    {
138 1
        return new Styles;
139
    }
140
141
    /**
142
     * Return api instance initialized with the spreadsheet ID.
143
     *
144
     * @return Api
145
     */
146 1
    public function api()
147
    {
148 1
        return $this->api->setSpreadsheetId($this->getId());
149
    }
150
}
151