Code Duplication    Length = 32-34 lines in 2 locations

src/Xtools/EditCounterRepository.php 2 locations

@@ 443-476 (lines=34) @@
440
     * @param User $user The user.
441
     * @return string[]
442
     */
443
    public function getMonthCounts(Project $project, User $user)
444
    {
445
        $cacheKey = "monthcounts.".$user->getCacheKey();
446
        $this->stopwatch->start($cacheKey, 'XTools');
447
        if ($this->cache->hasItem($cacheKey)) {
448
            return $this->cache->getItem($cacheKey)->get();
449
        }
450
451
        $username = $user->getUsername();
452
        $revisionTable = $this->getTableName($project->getDatabaseName(), 'revision');
453
        $pageTable = $this->getTableName($project->getDatabaseName(), 'page');
454
        $sql =
455
            "SELECT "
456
            . "     YEAR(rev_timestamp) AS `year`,"
457
            . "     MONTH(rev_timestamp) AS `month`,"
458
            . "     page_namespace,"
459
            . "     COUNT(rev_id) AS `count` "
460
            .  " FROM $revisionTable JOIN $pageTable ON (rev_page = page_id)"
461
            . " WHERE rev_user_text = :username"
462
            . " GROUP BY YEAR(rev_timestamp), MONTH(rev_timestamp), page_namespace "
463
            . " ORDER BY rev_timestamp DESC";
464
        $resultQuery = $this->getProjectsConnection()->prepare($sql);
465
        $resultQuery->bindParam(":username", $username);
466
        $resultQuery->execute();
467
        $totals = $resultQuery->fetchAll();
468
469
        $cacheItem = $this->cache->getItem($cacheKey);
470
        $cacheItem->expiresAfter(new DateInterval('PT10M'));
471
        $cacheItem->set($totals);
472
        $this->cache->save($cacheItem);
473
474
        $this->stopwatch->stop($cacheKey);
475
        return $totals;
476
    }
477
478
    /**
479
     * Get yearly edit totals for this user, grouped by namespace.
@@ 484-515 (lines=32) @@
481
     * @param User $user The user.
482
     * @return string[] ['<namespace>' => ['<year>' => 'total', ... ], ... ]
483
     */
484
    public function getYearCounts(Project $project, User $user)
485
    {
486
        $cacheKey = "yearcounts.".$user->getCacheKey();
487
        $this->stopwatch->start($cacheKey, 'XTools');
488
        if ($this->cache->hasItem($cacheKey)) {
489
            return $this->cache->getItem($cacheKey)->get();
490
        }
491
492
        $username = $user->getUsername();
493
        $revisionTable = $this->getTableName($project->getDatabaseName(), 'revision');
494
        $pageTable = $this->getTableName($project->getDatabaseName(), 'page');
495
        $sql = "SELECT "
496
            . "     YEAR(rev_timestamp) AS `year`,"
497
            . "     page_namespace,"
498
            . "     COUNT(rev_id) AS `count` "
499
            . " FROM $revisionTable JOIN $pageTable ON (rev_page = page_id)"
500
            . " WHERE rev_user_text = :username"
501
            . " GROUP BY YEAR(rev_timestamp), page_namespace "
502
            . " ORDER BY rev_timestamp ASC ";
503
        $resultQuery = $this->getProjectsConnection()->prepare($sql);
504
        $resultQuery->bindParam(":username", $username);
505
        $resultQuery->execute();
506
        $totals = $resultQuery->fetchAll();
507
508
        $cacheItem = $this->cache->getItem($cacheKey);
509
        $cacheItem->set($totals);
510
        $cacheItem->expiresAfter(new DateInterval('P10M'));
511
        $this->cache->save($cacheItem);
512
513
        $this->stopwatch->stop($cacheKey);
514
        return $totals;
515
    }
516
517
    /**
518
     * Get data for the timecard chart, with totals grouped by day and to the nearest two-hours.