diff --git a/app/module/Statistics/src/Calculator/AveragePostsPerUserPerMonth.php b/app/module/Statistics/src/Calculator/AveragePostsPerUserPerMonth.php new file mode 100644 index 0000000..d90663c --- /dev/null +++ b/app/module/Statistics/src/Calculator/AveragePostsPerUserPerMonth.php @@ -0,0 +1,56 @@ +getDate()->format('M, Y'); + + $this->postTotals[$key] = ($this->postTotals[$key] ?? 0) + 1; + + if (!in_array($postTo->getAuthorId(), $this->userList)) { + $this->userList[] = $postTo->getAuthorId(); + } + } + + /** + * @inheritDoc + */ + protected function doCalculate(): StatisticsTo + { + $averagePostsPerUserPerMonth = 0; + if (!empty($this->postTotals) && !empty($this->userList)) { + $totalPosts = array_sum($this->postTotals); + $numberOfMonths = count($this->postTotals); + $numberOfAuthors = count($this->userList); + $averagePostsPerUserPerMonth = round($totalPosts / $numberOfAuthors / $numberOfMonths, 1); + } + + return (new StatisticsTo()) + ->setName($this->parameters->getStatName()) + ->setValue($averagePostsPerUserPerMonth) + ->setUnits(self::UNITS); + } +} diff --git a/app/module/Statistics/src/Calculator/Factory/StatisticsCalculatorFactory.php b/app/module/Statistics/src/Calculator/Factory/StatisticsCalculatorFactory.php index 096c4ce..e58ed07 100644 --- a/app/module/Statistics/src/Calculator/Factory/StatisticsCalculatorFactory.php +++ b/app/module/Statistics/src/Calculator/Factory/StatisticsCalculatorFactory.php @@ -7,7 +7,7 @@ use Statistics\Calculator\CalculatorComposite; use Statistics\Calculator\CalculatorInterface; use Statistics\Calculator\MaxPostLength; -use Statistics\Calculator\NoopCalculator; +use Statistics\Calculator\AveragePostsPerUserPerMonth; use Statistics\Calculator\TotalPostsPerWeek; use Statistics\Dto\ParamsTo; use Statistics\Enum\StatsEnum; @@ -24,7 +24,7 @@ class StatisticsCalculatorFactory StatsEnum::AVERAGE_POST_LENGTH => AveragePostLength::class, StatsEnum::MAX_POST_LENGTH => MaxPostLength::class, StatsEnum::TOTAL_POSTS_PER_WEEK => TotalPostsPerWeek::class, - StatsEnum::AVERAGE_POSTS_NUMBER_PER_USER_PER_MONTH => NoopCalculator::class, + StatsEnum::AVERAGE_POSTS_NUMBER_PER_USER_PER_MONTH => AveragePostsPerUserPerMonth::class, ]; /** diff --git a/app/module/Statistics/src/Calculator/NoopCalculator.php b/app/module/Statistics/src/Calculator/NoopCalculator.php deleted file mode 100644 index 6790ef3..0000000 --- a/app/module/Statistics/src/Calculator/NoopCalculator.php +++ /dev/null @@ -1,27 +0,0 @@ -getTestPosts(); + $params = $this->getParams('2018-08-01 00:00:00', '2018-08-31 23:59:59'); + + $averagePostsPerUserPerMonth = $this->getAveragePostsPerUserPerMonth($posts, $params); + + $this->assertEquals(1, $averagePostsPerUserPerMonth); + } + + /** + * @test + */ + public function testStatsForTwoMonths(): void + { + $posts = $this->getTestPosts(); + $params = $this->getParams('2018-08-01 00:00:00', '2018-09-30 23:59:59'); + + $averagePostsPerUserPerMonth = $this->getAveragePostsPerUserPerMonth($posts, $params); + + $this->assertEquals(0.7, $averagePostsPerUserPerMonth); + } + + /** + * @test + */ + public function testForNoPosts(): void + { + $posts = []; + $params = $this->getParams('2018-08-01 00:00:00', '2018-08-31 23:59:59'); + + $averagePostsPerUserPerMonth = $this->getAveragePostsPerUserPerMonth($posts, $params); + + $this->assertEquals(0, $averagePostsPerUserPerMonth); + } + + private function getTestPosts() + { + $postsJson = file_get_contents(self::TEST_FILE); + $responseData = json_decode($postsJson, true); + + return $responseData['data']['posts']; + } + + private function getAveragePostsPerUserPerMonth($posts, $params) + { + $posts = $this->fetchPosts($posts); + $stats = $this->getStats($params, $posts); + + return $stats->getChildren()[0]->getValue(); + } + + private function getParams($start, $end): array + { + $startDate = DateTime::createFromFormat('Y-m-d H:i:s', $start); + $endDate = DateTime::createFromFormat('Y-m-d H:i:s', $end); + + return [ + (new ParamsTo()) + ->setStatName(StatsEnum::AVERAGE_POSTS_NUMBER_PER_USER_PER_MONTH) + ->setStartDate($startDate) + ->setEndDate($endDate) + ]; + } + + private function getStats($params, $posts) + { + $statsService = StatisticsServiceFactory::create(); + + return $statsService->calculateStats($posts, $params); + } + + private function fetchPosts($posts) : Traversable { + $hydrator = new FictionalPostHydrator(); + + foreach ($posts as $postData) { + yield $hydrator->hydrate($postData); + } + } +} diff --git a/app/tests/unit/TestTest.php b/app/tests/unit/TestTest.php deleted file mode 100644 index 1ebb8a6..0000000 --- a/app/tests/unit/TestTest.php +++ /dev/null @@ -1,23 +0,0 @@ -assertTrue(true); - } -}