Skip to content

Commit 8bca905

Browse files
committed
Initial version of CsvProcessor.
1 parent 80276ec commit 8bca905

File tree

3 files changed

+317
-0
lines changed

3 files changed

+317
-0
lines changed

src/Gwt/Processor/CsvWriter.php

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,215 @@
1+
<?php
2+
/**
3+
* PHP class for saving CSV files from Google Webmaster Tools.
4+
*
5+
* This class does NOT require the Zend gdata package be installed
6+
* in order to run.
7+
*
8+
* Copyright 2012 eyecatchUp UG. All Rights Reserved.
9+
*
10+
* Licensed under the Apache License, Version 2.0 (the "License");
11+
* you may not use this file except in compliance with the License.
12+
* You may obtain a copy of the License at
13+
*
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing, software
17+
* distributed under the License is distributed on an "AS IS" BASIS,
18+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
* See the License for the specific language governing permissions and
20+
* limitations under the License.
21+
*
22+
* @author Anton Kolenkov <dorantor@gmail.com>
23+
* @author Stephan Schmitz <eyecatchup@gmail.com>
24+
* @link https://github.com/dorantor/php-webmaster-tools-downloads/
25+
* @link https://github.com/eyecatchup/php-webmaster-tools-downloads/
26+
*/
27+
28+
class Gwt_Processor_CsvWriter
29+
extends Gwt_Processor_ProcessorAbstract
30+
{
31+
/**
32+
* Path for saving files
33+
*
34+
* @var string
35+
*/
36+
private $_savePath = '.';
37+
38+
/**
39+
* Date format used in filename
40+
*
41+
* @var string
42+
*/
43+
private $_dateFormat = 'Ymd-His';
44+
45+
/**
46+
* Template used to build filename
47+
*
48+
* @var string
49+
*/
50+
private $_filenameTemplate = '{website}-{tableName}-{date}.csv';
51+
52+
/**
53+
* Process data
54+
*
55+
* @param mixed $data
56+
* @param string $tableName
57+
* @return mixed
58+
*/
59+
public function process($data, $tableName)
60+
{
61+
$filename = $this->getSavePath() . DIRECTORY_SEPARATOR . $this->buildFilename($tableName);
62+
63+
if ($this->saveData($data, $filename)) {
64+
return $filename;
65+
}
66+
67+
return null;
68+
}
69+
70+
/**
71+
* Set path where to save files
72+
*
73+
* @param $savePath
74+
* @return $this
75+
*/
76+
public function setSavePath($savePath)
77+
{
78+
$this->_savePath = $savePath;
79+
80+
return $this;
81+
}
82+
83+
/**
84+
* Get path where to save files
85+
*
86+
* @return string
87+
*/
88+
public function getSavePath()
89+
{
90+
return $this->_savePath;
91+
}
92+
93+
/**
94+
* Get filename template
95+
*
96+
* @return string
97+
*/
98+
public function getFilenameTemplate()
99+
{
100+
return $this->_filenameTemplate;
101+
}
102+
103+
/**
104+
* Set template for filename
105+
*
106+
* @param $template
107+
* @return $this
108+
*/
109+
public function setFilenameTemplate($template)
110+
{
111+
$this->_filenameTemplate = $template;
112+
113+
return $this;
114+
}
115+
116+
/**
117+
* Get date format
118+
*
119+
* @return string
120+
*/
121+
public function getDateFormat()
122+
{
123+
return $this->_dateFormat;
124+
}
125+
126+
/**
127+
* Set date format to be used in file name
128+
*
129+
* @param $dateFormat
130+
* @return $this
131+
*/
132+
public function setDateFormat($dateFormat)
133+
{
134+
$this->_dateFormat = $dateFormat;
135+
136+
return $this;
137+
}
138+
139+
/**
140+
* Set datetime to be used in filename
141+
*
142+
* @todo use clients dateStart-dateEnd
143+
* @param DateTime $dt
144+
* @return void
145+
*/
146+
public function setDateTime(DateTime $dt)
147+
{
148+
$this->_dt = $dt;
149+
}
150+
151+
/**
152+
* Get datetime to be used in filename
153+
*
154+
* @todo use clients dateStart-dateEnd
155+
* @return DateTime
156+
*/
157+
public function getDateTime()
158+
{
159+
if (null !== $this->_dt) {
160+
return $this->_dt;
161+
}
162+
163+
return new DateTime('now', new DateTimeZone('UTC'));
164+
}
165+
166+
/**
167+
* Build filename using template
168+
*
169+
* @param $tableName
170+
* @return mixed|string
171+
*/
172+
protected function buildFilename($tableName)
173+
{
174+
$filename = $this->getFilenameTemplate();
175+
$filename = str_replace(
176+
'{website}',
177+
parse_url($this->getClient()->getWebsite(), PHP_URL_HOST),
178+
$filename
179+
);
180+
$filename = str_replace('{tableName}', $tableName, $filename);
181+
$filename = str_replace(
182+
'{date}',
183+
$this->getDateTime()->format($this->getDateFormat()),
184+
$filename
185+
);
186+
187+
return $filename;
188+
}
189+
190+
/**
191+
* Saves data to a CSV file
192+
* Tries to create directories if possible. Use umask() to change default 0777 permissions.
193+
*
194+
* @see umask()
195+
* @param string $data Downloaded CSV data
196+
* @param string $finalName path with target filename
197+
* @return bool
198+
* @todo should throw specific exceptions
199+
*/
200+
private function saveData($data, $finalName)
201+
{
202+
$dir = dirname($finalName);
203+
if (!is_dir($dir)) {
204+
if (!mkdir($dir, 0777, true)) {
205+
return false;
206+
}
207+
}
208+
209+
if (strlen($data) > 1 && file_put_contents($finalName, utf8_decode($data))) {
210+
return true;
211+
} else {
212+
return false;
213+
}
214+
}
215+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
/**
3+
* PHP class for downloading CSV files from Google Webmaster Tools.
4+
*
5+
* This class does NOT require the Zend gdata package be installed
6+
* in order to run.
7+
*
8+
* Copyright 2015 Anton Kolenkov. All Rights Reserved.
9+
*
10+
* Licensed under the Apache License, Version 2.0 (the "License");
11+
* you may not use this file except in compliance with the License.
12+
* You may obtain a copy of the License at
13+
*
14+
* http://www.apache.org/licenses/LICENSE-2.0
15+
*
16+
* Unless required by applicable law or agreed to in writing, software
17+
* distributed under the License is distributed on an "AS IS" BASIS,
18+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19+
* See the License for the specific language governing permissions and
20+
* limitations under the License.
21+
*
22+
* @author Anton Kolenkov <dorantor@gmail.com>
23+
* @link https://code.google.com/p/php-webmaster-tools-downloads/
24+
* @link https://github.com/dorantor/php-webmaster-tools-downloads/
25+
*/
26+
27+
/**
28+
* Class with common logic for processors
29+
*/
30+
abstract class Gwt_Processor_ProcessorAbstract
31+
implements Gwt_Processor_ProcessorInterface
32+
{
33+
/**
34+
* Set GWT client
35+
*
36+
* @param Gwt_Client $client
37+
* @return void
38+
*/
39+
public function setClient(Gwt_Client $client)
40+
{
41+
$this->_client = $client;
42+
}
43+
44+
/**
45+
* Get GWT client
46+
*
47+
* @return mixed
48+
*/
49+
public function getClient()
50+
{
51+
return $this->_client;
52+
}
53+
54+
/**
55+
* Create processor object and set params
56+
*
57+
* @throws Exception
58+
* @param array $params
59+
* @return static
60+
*/
61+
public static function factory(array $params)
62+
{
63+
$processor = new static();
64+
foreach ($params as $param => $value) {
65+
$method = 'set' . $param;
66+
if (!method_exists($processor, $method)) {
67+
throw new Exception('Not supported param: ' . var_export($param, true));
68+
}
69+
$processor->$method($value);
70+
}
71+
72+
return $processor;
73+
}
74+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
interface Gwt_Processor_ProcessorInterface
4+
{
5+
/**
6+
* Set GWT client
7+
*
8+
* @param Gwt_Client $client
9+
* @return void
10+
*/
11+
public function setClient(Gwt_Client $client);
12+
13+
/**
14+
* Get GWT client
15+
*
16+
* @return mixed
17+
*/
18+
public function getClient();
19+
20+
/**
21+
* Process data
22+
*
23+
* @param mixed $data
24+
* @param string $tableName
25+
* @return mixed
26+
*/
27+
public function process($data, $tableName);
28+
}

0 commit comments

Comments
 (0)