Update poll command to use services.

This commit is contained in:
Marco Vito Moscaritolo 2015-05-31 11:33:44 +02:00
parent 86de54b658
commit a461fb580d
2 changed files with 47 additions and 22 deletions

View file

@ -9,15 +9,16 @@
namespace PHPCI\Command;
use b8\Store\Factory;
use b8\HttpClient;
use Monolog\Logger;
use PHPCI\Helper\Lang;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Yaml\Parser;
use PHPCI\Config;
use PHPCI\Model\Build;
use PHPCI\Store\BuildStore;
use PHPCI\Store\ProjectStore;
/**
* Run console command - Poll github for latest commit id
@ -28,15 +29,39 @@ use PHPCI\Model\Build;
class PollCommand extends Command
{
/**
* @var \Monolog\Logger
* @var Config
*/
protected $config;
/**
* @var Logger
*/
protected $logger;
public function __construct(Logger $logger)
/**
* @var BuildStore
*/
protected $buildStore;
/**
* @var ProjectStore
*/
protected $projectStore;
/**
* @var HttpClient
*/
protected $githubClient;
public function __construct(Config $config, Logger $logger, BuildStore $buildStore, ProjectStore $projectStore, HttpClient $githubClient)
{
parent::__construct();
$this->config = $config;
$this->logger = $logger;
$this->buildStore = $buildStore;
$this->projectStore = $projectStore;
$this->githubClient = $githubClient;
}
protected function configure()
@ -51,27 +76,19 @@ class PollCommand extends Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$parser = new Parser();
$yaml = file_get_contents(APPLICATION_PATH . 'PHPCI/config.yml');
$this->settings = $parser->parse($yaml);
$token = $this->settings['phpci']['github']['token'];
$token = $this->config->get('phpci.github.token');
if (!$token) {
$this->logger->error(Lang::get('no_token'));
return;
}
$buildStore = Factory::getStore('Build');
$this->logger->addInfo(Lang::get('finding_projects'));
$projectStore = Factory::getStore('Project');
$result = $projectStore->getWhere();
$result = $this->projectStore->getWhere();
$this->logger->addInfo(Lang::get('found_n_projects', count($result['items'])));
foreach ($result['items'] as $project) {
$http = new HttpClient('https://api.github.com');
$commits = $http->get('/repos/' . $project->getReference() . '/commits', array('access_token' => $token));
$commits = $this->githubClient->get('/repos/' . $project->getReference() . '/commits', array('access_token' => $token));
$last_commit = $commits['body'][0]['sha'];
$last_committer = $commits['body'][0]['commit']['committer']['email'];
@ -90,14 +107,14 @@ class PollCommand extends Command
$build->setStatus(Build::STATUS_NEW);
$build->setBranch($project->getBranch());
$build->setCreated(new \DateTime());
$build->setCommitMessage($message);
$build->setCommitMessage($message);
if (!empty($last_committer)) {
$build->setCommitterEmail($last_committer);
}
$buildStore->save($build);
$this->buildStore->save($build);
$project->setLastCommit($last_commit);
$projectStore->save($project);
$this->projectStore->save($project);
}
}

View file

@ -6,22 +6,22 @@ parameters:
services:
storage.user:
class: PHPCI\Store\Base\UserStore
class: PHPCI\Store\UserStore
factory: [%storage.factory%, getStore]
arguments:
- User
storage.project:
class: PHPCI\Store\Base\ProjectStore
class: PHPCI\Store\ProjectStore
factory: [%storage.factory%, getStore]
arguments:
- Project
storage.build:
class: PHPCI\Store\Base\BuildStore
class: PHPCI\Store\BuildStore
factory: [%storage.factory%, getStore]
arguments:
- Build
storage.build_meta:
class: PHPCI\Store\Base\BuildMetaStore
class: PHPCI\Store\BuildMetaStore
factory: [%storage.factory%, getStore]
arguments:
- BuildMeta
@ -31,6 +31,10 @@ services:
http.response:
class: b8\Http\Response
arguments: []
http_client.github:
class: b8\HttpClient
arguments:
- https://api.github.com
config:
class: PHPCI\Config
arguments: [%config_file%]
@ -85,7 +89,11 @@ services:
console.command.poll:
class: PHPCI\Command\PollCommand
arguments:
- @config
- @console.logger
- @storage.build
- @storage.project
- @http_client.github
console.command.create_admin:
class: PHPCI\Command\CreateAdminCommand
arguments: