Completed
Push — master ( 815517...75567a )
by Nassif
13:36
created

Spreadsheet   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Test Coverage

Coverage 36.36%

Importance

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

17 Methods

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