Skip to content

Commit

Permalink
Data Pusher for static web-sites
Browse files Browse the repository at this point in the history
  • Loading branch information
boryashkin committed Jan 29, 2017
1 parent 602b118 commit 9054665
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
102 changes: 102 additions & 0 deletions components/FtpPusher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace app\components;
use yii\base\Exception;
use yii\web\UploadedFile;

/**
* Set as component?
*
* Class FtpPusher
* @package app\components
*/
class FtpPusher
{
const LOCAL_STORAGE = __DIR__ . '/pushed/';

private $host;
private $login;
private $password;
private $connection;

private $files = [
//[0 => filename
//1 => localpath
//2 => remotepath
//3 => uploaded?]
];

public function __construct($host, $login, $password)
{
$this->login = $login;
$this->host = $host;
$this->password = $password;
}

public function connectProcedure()
{
if (!$this->connect()) {
throw new Exception('Connection failed');
}
if (!$this->login()) {
$this->disconnect();
throw new Exception('Authorization failed');
}
}

private function connect()
{
$this->connection = ftp_connect($this->host, 21, 30);

return $this->connection !== false;
}
private function disconnect()
{
ftp_close($this->connection);
$this->connection = null;
}

private function login()
{
return @ftp_login($this->connection, $this->login, $this->password);
}

public function push()
{
$this->connectProcedure();

$response = true;
foreach ($this->files as $key => $file) {
if ($file[3]) {
//already uploaded
continue;
}
if (ftp_put($this->connection, $file[2] . $file[0], $file[1] . $file[0], FTP_ASCII)){
$this->files[$key][3] = true;
} else {
$this->files[$key][3] = false;
$response = false;
}
}

$this->disconnect();

return $response;
}

public function addFromString($content, $remotePath, $name)
{
if (file_put_contents(self::LOCAL_STORAGE . $name, $content)) {
$this->files[] = [
$name,
self::LOCAL_STORAGE,
$remotePath,
false,
];

return true;
}

return false;
}
}
50 changes: 50 additions & 0 deletions components/Pusher.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace app\components;

/**
* Class Pusher
* @package app\components
*/
abstract class Pusher
{
private $transport;

/**
* @todo: create interface for transport
* Pusher constructor.
* @param $transport
*/
public function __construct($transport)
{
$this->transport = $transport;
}

/**
* @param $content
* @param $name
* @param $extension
* @return mixed
*/
public function addFileByString($content, $name, $extension)
{

}

/**Add from file system
* @return mixed
*/
public function addFileByPath()
{

}

/**
* Send files
* @return mixed
*/
public function push()
{
$this->transport->push();
}
}
15 changes: 15 additions & 0 deletions modules/wallet/models/OperationQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace app\modules\wallet\models;
use yii\db\Expression;
use yii\db\Query;

/**
* This is the ActiveQuery class for [[Operation]].
Expand Down Expand Up @@ -62,4 +63,18 @@ public function getTagTotals()
->orWhere('{{operation}}.creditId IS NULL')
->groupBy('{{tag}}.id')->asArray()->all();
}

public function getBorisdStat()
{
$sql = <<<SQL
SELECT
(SELECT count(sum) FROM operation WHERE description LIKE "%чай%") as tea,
(SELECT count(sum) FROM operation WHERE description LIKE "%хлеб%") as bread,
(SELECT count(sum) FROM operation WHERE description LIKE "%кетчуп%") as ketchup,
(SELECT count(sum) FROM operation WHERE description LIKE "%яйц%") as egg
FROM dual
SQL;

return \Yii::$app->db->createCommand($sql)->queryOne();
}
}

0 comments on commit 9054665

Please sign in to comment.