Skip to content
This repository has been archived by the owner on Sep 23, 2022. It is now read-only.

Commit

Permalink
Big refactoring:
Browse files Browse the repository at this point in the history
- refactor code of Updater,
- introduce activities instead of texts,
- removed useless exception,
- refatored creation of bundle into one BundleManager,
- fixed and simplified creation of owners for background processes,
- removed unused tags from bundle
- fixed incomplete tests!
  • Loading branch information
stloyd committed Oct 11, 2012
1 parent ed464e7 commit 9378f46
Show file tree
Hide file tree
Showing 42 changed files with 1,597 additions and 830 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.markdown
@@ -1,3 +1,20 @@
* 2012-10-08
* Added `Activity` entity and replace all text based ones with new approach
in `Bundle` and `Developer` entities,
* Added `OwnerRepository#findOneByUniqueFields()` to lookup for users in database,
* Added new methods to `BundleUtilsExtension`: `#bundleActivityIcon`
and `#bundleActivityMessage`,
* Added `BundleManager` for easier creation of bundles,
* Moved `OwnerManager` into own namespace,
* Moved methods `#getGithubId()`, `#getSensioId()` from `Developer` entity
to `Owner` entity
* Replace dependency of `Github\Organization` from `OrganizationRepository`
to `OwnerManager`,
* Replaced dependency of `Update` from `OwnerManager` to `BundleManager`,
* Removed `Updater#setUp` method, bundles are now loaded via `Pagerfanta`,
* `UpdateBundleConsumer` no longer removes bundle on API failure,
* Removed unused exception `UserNotFoundException`

* 2012-10-03
* Removed `js` format from web API, we still support: `json` and `html`,
* Moved code related to web API into controllers and removed all view templates
Expand Down
34 changes: 34 additions & 0 deletions app/DoctrineMigrations/Version20121010101749.php
@@ -0,0 +1,34 @@
<?php

namespace Application\Migrations;

use Doctrine\DBAL\Migrations\AbstractMigration,
Doctrine\DBAL\Schema\Schema;

/**
* Auto-generated Migration: Please modify to your need!
*/
class Version20121010101749 extends AbstractMigration
{
public function up(Schema $schema)
{
// this up() migration is autogenerated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");

$this->addSql("CREATE TABLE activities (id INT AUTO_INCREMENT NOT NULL, bundle_id INT DEFAULT NULL, developer_id INT DEFAULT NULL, type INT NOT NULL, state INT NOT NULL, createdAt DATETIME NOT NULL, message VARCHAR(255) DEFAULT NULL, author VARCHAR(255) DEFAULT NULL, bundleName VARCHAR(255) DEFAULT NULL, bundleOwnerName VARCHAR(255) DEFAULT NULL, INDEX IDX_B5F1AFE5F1FAD9D3 (bundle_id), INDEX IDX_B5F1AFE564DD9267 (developer_id), INDEX type_state (type, state), INDEX created_at (createdAt), PRIMARY KEY(id)) ENGINE = InnoDB");
$this->addSql("ALTER TABLE activities ADD CONSTRAINT FK_B5F1AFE5F1FAD9D3 FOREIGN KEY (bundle_id) REFERENCES bundle (id)");
$this->addSql("ALTER TABLE activities ADD CONSTRAINT FK_B5F1AFE564DD9267 FOREIGN KEY (developer_id) REFERENCES owner (id)");
$this->addSql("ALTER TABLE owner ADD lastCommitAt DATE DEFAULT NULL");
$this->addSql("ALTER TABLE bundle DROP lastCommits, DROP tags");
}

public function down(Schema $schema)
{
// this down() migration is autogenerated, please modify it to your needs
$this->abortIf($this->connection->getDatabasePlatform()->getName() != "mysql");

$this->addSql("DROP TABLE activities");
$this->addSql("ALTER TABLE bundle ADD lastCommits LONGTEXT NOT NULL, ADD tags LONGTEXT NOT NULL");
$this->addSql("ALTER TABLE owner DROP lastCommitAt");
}
}
Expand Up @@ -34,18 +34,13 @@ protected function execute(InputInterface $input, OutputInterface $output)

/* @var $updater Updater */
$updater = $container->get('knp_bundles.updater');
$updater->setOutput($output);

if (!$input->getOption('no-publish')) {
// manually set RabbitMQ producer
$updater->setBundleUpdateProducer($container->get('old_sound_rabbit_mq.update_bundle_producer'));
}

$updater->setOutput($output);
$updater->setUp();

$updater->addBundle($input->getArgument('bundleName'));

$em = $this->getContainer()->get('knp_bundles.entity_manager');
$em->flush();
}
}
Expand Up @@ -34,7 +34,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
/* @var $updater Updater */
$updater = $this->getContainer()->get('knp_bundles.updater');
$updater->setOutput($output);
$updater->setUp();

$bundles = $updater->searchNewBundles();
$updater->createMissingBundles($bundles);
Expand Down
Expand Up @@ -36,15 +36,13 @@ protected function execute(InputInterface $input, OutputInterface $output)

/* @var $updater Updater */
$updater = $container->get('knp_bundles.updater');
$updater->setOutput($output);

if (!$input->getOption('no-publish')) {
// manually set RabbitMQ producer
$updater->setBundleUpdateProducer($container->get('old_sound_rabbit_mq.update_bundle_producer'));
}

$updater->setOutput($output);
$updater->setUp();

$bundles = $updater->searchNewBundles();
$updater->createMissingBundles($bundles);
$updater->updateBundlesData();
Expand Down
Expand Up @@ -33,15 +33,13 @@ protected function execute(InputInterface $input, OutputInterface $output)

/* @var $updater Updater */
$updater = $container->get('knp_bundles.updater');
$updater->setOutput($output);

if (!$input->getOption('no-publish')) {
// manually set RabbitMQ producer
$updater->setBundleUpdateProducer($container->get('old_sound_rabbit_mq.update_bundle_producer'));
}

$updater->setOutput($output);
$updater->setUp();

$updater->updateBundlesData();

$em = $container->get('knp_bundles.entity_manager');
Expand Down
Expand Up @@ -30,19 +30,19 @@ protected function execute(InputInterface $input, OutputInterface $output)
$em = $this->getContainer()->get('knp_bundles.entity_manager');

if ($em->getConnection()->getDatabasePlatform()->getName() == 'sqlite') {
$output->writeln(sprintf('[%s] This command can\'t be executed on <error>SQLite</error>!', $this->currentTime()));
$output->writeln(sprintf('[%s] This command can\'t be executed on <error>SQLite</error>!', date('d-m-y H:i:s')));

return 1;
}

$em->getConnection()->beginTransaction();
try {
$nbRows = $this->updateTrends();
$output->writeln(sprintf('[%s] <info>%s</info> rows updated', $this->currentTime(), $nbRows));
$output->writeln(sprintf('[%s] <info>%s</info> rows updated', date('d-m-y H:i:s'), $nbRows));

$em->getConnection()->commit();
} catch (\Exception $e) {
$output->writeln(sprintf('[%s] <error>Rollbacking</error> because of %s', $this->currentTime(), $e));
$output->writeln(sprintf('[%s] <error>Rollbacking</error> because of %s', date('d-m-y H:i:s'), $e));
$em->getConnection()->rollback();
$em->close();

Expand Down Expand Up @@ -89,9 +89,4 @@ private function updateTrends()

return $nbRows;
}

private function currentTime()
{
return date('d-m-y H:i:s');
}
}
66 changes: 66 additions & 0 deletions src/Knp/Bundle/KnpBundlesBundle/Command/MigrateCommitsCommand.php
@@ -0,0 +1,66 @@
<?php

namespace Knp\Bundle\KnpBundlesBundle\Command;

use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Output\NullOutput;

use Pagerfanta\Pagerfanta;
use Pagerfanta\Adapter\DoctrineORMAdapter;

use Knp\Bundle\KnpBundlesBundle\Entity\Bundle;
use Knp\Bundle\KnpBundlesBundle\Github\Repo;

/**
* @todo Remove this command on next week or so
*/
class MigrateCommitsCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setDefinition(array())
->setName('kb:migrate:commits')
;
}

protected function execute(InputInterface $input, OutputInterface $output)
{
/** @var $repo Repo */
$em = $this->getContainer()->get('doctrine.orm.entity_manager');
$repo = $this->getContainer()->get('knp_bundles.github_repository_api');

$page = 1;
$pager = new Pagerfanta(new DoctrineORMAdapter($em->getRepository('KnpBundlesBundle:Bundle')->queryAllSortedBy('updatedAt'), false));
$pager
->setMaxPerPage(100)
->setCurrentPage($page, false, true)
;

if (1 === $page) {
$this->output->writeln(sprintf('[%s] Loaded <comment>%d</comment> bundles from the DB', date('d-m-y H:i:s'), $pager->getNbResults()));
}

do {
/** @var $bundle Bundle */
foreach ($pager as $bundle) {
// Check that API not failed
if (!$repo->updateCommits($bundle)) {
// Sleep a while, and check again
sleep(60);

$repo->updateCommits($bundle);
}
}

$this->output->writeln(sprintf('[%s] Migrated %d from %d bundles', date('d-m-y H:i:s'), $page * 100, $pager->getNbResults()));

++$page;
} while ($pager->haveToPaginate() && $pager->setCurrentPage($page, false, true));

return 0;
}
}
Expand Up @@ -7,8 +7,8 @@
use Knp\Bundle\KnpBundlesBundle\Entity\Bundle;
use Knp\Bundle\KnpBundlesBundle\Entity\Score;
use Knp\Bundle\KnpBundlesBundle\Entity\Owner;
use Knp\Bundle\KnpBundlesBundle\Entity\OwnerManager;
use Knp\Bundle\KnpBundlesBundle\Indexer\SolrIndexer;
use Knp\Bundle\KnpBundlesBundle\Manager\OwnerManager;
use Knp\Bundle\KnpBundlesBundle\Travis\Travis;

use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
Expand Down Expand Up @@ -43,22 +43,22 @@ class UpdateBundleConsumer implements ConsumerInterface
private $em;

/**
* @var Knp\Bundle\KnpBundlesBundle\Entity\OwnerManager
* @var OwnerManager
*/
private $ownerManager;

/**
* @var Knp\Bundle\KnpBundlesBundle\Indexer\SolrIndexer
* @var SolrIndexer
*/
private $indexer;

/**
* @var Knp\Bundle\KnpBundlesBundle\Github\Repo
* @var Repo
*/
private $githubRepoApi;

/**
* @var Knp\Bundle\KnpBundlesBundle\Travis\Travis
* @var Travis
*/
private $travis;

Expand Down Expand Up @@ -137,9 +137,8 @@ public function execute(AMQPMessage $msg)
try {
if (!$this->githubRepoApi->update($bundle)) {
if ($this->logger) {
$this->logger->warn(sprintf('Update failed, bundle "%s" will be removed', $bundle->getName()));
$this->logger->warn(sprintf('Update of "%s" failed', $bundle->getName()));
}
$this->removeBundle($bundle);

return;
}
Expand Down
58 changes: 19 additions & 39 deletions src/Knp/Bundle/KnpBundlesBundle/Controller/BundleController.php
Expand Up @@ -8,9 +8,9 @@
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Pagerfanta\Pagerfanta;
use Pagerfanta\Adapter\SolariumAdapter;
use Knp\Bundle\KnpBundlesBundle\Entity\Activity;
use Knp\Bundle\KnpBundlesBundle\Entity\Bundle;
use Knp\Bundle\KnpBundlesBundle\Entity\Developer;
use Knp\Bundle\KnpBundlesBundle\Updater\Exception\UserNotFoundException;

class BundleController extends BaseController
{
Expand Down Expand Up @@ -44,10 +44,9 @@ public function searchAction(Request $request)
}

// Skip search if query matches exactly one bundle name, and such was found in database
if (!$request->isXmlHttpRequest() && preg_match('/^[a-z0-9-]+\/[a-z0-9-\.]+$/i', $query)) {
if (!$request->isXmlHttpRequest() && preg_match('/^[a-z0-9-]+\/[a-z0-9-]+$/i', $query)) {
list($ownerName, $name) = explode('/', $query);

/* @var $bundle Bundle */
$bundle = $this->getRepository('Bundle')->findOneBy(array('ownerName' => $ownerName, 'name' => $name));
if ($bundle) {
return $this->redirect($this->generateUrl('bundle_show', array('ownerName' => $ownerName, 'name' => $name, '_format' => $format)));
Expand Down Expand Up @@ -223,7 +222,8 @@ public function listAction(Request $request, $sort)

$this->highlightMenu('bundles');

$owners = $this->getRepository('Developer')->findAllSortedBy('createdAt', 20);
$developers = $this->getRepository('Developer')->findAllSortedBy('createdAt', 20);
$activities = $this->getRepository('Activity')->findAllSortedBy('createdAt', 10);

$graphPeriod = $this->container->getParameter('knp_bundles.bundle.graph.main_page.period');

Expand All @@ -235,7 +235,8 @@ public function listAction(Request $request, $sort)
)
),
'bundles' => $paginator,
'developers' => $owners,
'developers' => $developers,
'activities' => $activities,
'sort' => $sort,
'sortLegends' => $this->sortLegends
));
Expand Down Expand Up @@ -301,7 +302,7 @@ public function addAction(Request $request)

if (!$error && ($request->isXmlHttpRequest() || 'POST' === $request->getMethod())) {
$bundle = trim(str_replace(array('http://github.com', 'https://github.com', '.git'), '', $bundle), '/');
if (preg_match('/^[a-z0-9-]+\/[a-z0-9-\.]+$/i', $bundle)) {
if (preg_match('/^[a-z0-9-]+\/[a-z0-9-]+$/i', $bundle)) {
list($ownerName, $name) = explode('/', $bundle);

$url = $this->generateUrl('bundle_show', array('ownerName' => $ownerName, 'name' => $name));
Expand All @@ -317,22 +318,14 @@ public function addAction(Request $request)
if (!$error) {
/** @var $updater \Knp\Bundle\KnpBundlesBundle\Updater\Updater */
$updater = $this->get('knp_bundles.updater');
$updater->setUp();
try {
$valid = $updater->addBundle($bundle, false, true);

if ($valid) {
if (!$request->isXmlHttpRequest()) {
return $this->redirect($url);
}
$message = '<strong>Hey, friend!</strong> Thanks for adding <a href="'.$url.'">your bundle</a> to our database!';
} else {
$error = true;
$message = 'Specified repo is not valid Symfony2 bundle!';
if ($updater->addBundle($bundle)) {
if (!$request->isXmlHttpRequest()) {
return $this->redirect($url);
}
} catch (UserNotFoundException $e) {
$message = '<strong>Hey, friend!</strong> Thanks for adding <a href="'.$url.'">your bundle</a> to our database!';
} else {
$error = true;
$message = 'Specified user was not found on GitHub.';
$message = 'Specified repository is not valid Symfony2 bundle!';
}
}
} else {
Expand Down Expand Up @@ -360,32 +353,19 @@ public function addAction(Request $request)
public function changeUsageStatusAction($ownerName, $name)
{
/* @var $bundle Bundle */
$bundle = $this->getRepository('Bundle')->findOneByOwnerNameAndName($ownerName, $name);
$bundle = $this->getRepository('Bundle')->findOneBy(array('ownerName' => $ownerName, 'name' => $name));
if (!$bundle) {
throw new NotFoundHttpException(sprintf('The bundle "%s/%s" does not exist', $ownerName, $name));
}

$params = array('ownerName' => $ownerName, 'name' => $name);

if (!$owner = $this->get('security.context')->getToken()->getUser()) {
return $this->redirect($this->generateUrl('bundle_show', $params));
}
$em = $this->get('doctrine')->getEntityManager();

if ($owner->isUsingBundle($bundle)) {
$bundle->updateScore(-5);

$bundle->removeRecommender($owner);
} else {
$bundle->updateScore(5);

$bundle->addRecommender($owner);
$url = $this->generateUrl('bundle_show', array('ownerName' => $ownerName, 'name' => $name));
if (!$developer = $this->get('security.context')->getToken()->getUser()) {
return $this->redirect($url);
}

$em->persist($bundle);
$em->flush();
$this->get('knp_bundles.bundle.manager')->manageBundleRecommendation($bundle, $developer);

return $this->redirect($this->generateUrl('bundle_show', $params));
return $this->redirect($url);
}

public function searchByKeywordAction(Request $request, $slug)
Expand Down

0 comments on commit 9378f46

Please sign in to comment.