Skip to content

Commit 5a5b500

Browse files
committed
WIP - combine multiple requests into one
1 parent 39b06f2 commit 5a5b500

File tree

10 files changed

+325
-42
lines changed

10 files changed

+325
-42
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@
126126
},
127127
"require-dev": {
128128
"dbrekelmans/bdi": "^1.1",
129+
"dg/bypass-finals": "^1.6",
129130
"doctrine/doctrine-fixtures-bundle": "^3.5",
130131
"phpunit/phpunit": "^11.0",
131132
"symfony/browser-kit": "7.0.*",

composer.lock

Lines changed: 74 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rector.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@
2020

2121
$config->symfonyContainerXml(__DIR__.'/var/cache/test/App_KernelTestDebugContainer.xml');
2222
$config->symfonyContainerPhp(__DIR__.'/tests/rector-bootstrap.php');
23+
// $config->singleton(\Zenstruck\Foundry\Utils\Rector\PersistenceResolver::class,
24+
// static fn() => new \Zenstruck\Foundry\Utils\Rector\PersistenceResolver(
25+
// (require __DIR__.'/tests/rector-bootstrap.php')->get('doctrine')->getManager())
26+
// );
2327

2428
$config->sets([
29+
// \Zenstruck\Foundry\Utils\Rector\FoundrySetList::UP_TO_FOUNDRY_2,
2530
LevelSetList::UP_TO_PHP_83,
2631
SetList::DEAD_CODE,
2732
SymfonySetList::SYMFONY_64,

src/Controller/TodoListController.php

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,13 @@
66
use App\Entity\TodoList;
77
use App\Exception\HttpClientException;
88
use App\Form\TodoListType;
9-
use App\Model\GitHubIssue;
10-
use App\Model\GitHubPullRequest;
9+
use App\Model\GitHubUrlData;
10+
use App\Repository\TaskRepository;
1111
use App\Repository\TodoListRepository;
1212
use App\Security\Voter\TodoListVoter;
1313
use App\Service\TaskService;
1414
use App\Util\UrlParser;
15+
use Doctrine\Common\Collections\Collection;
1516
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1617
use Symfony\Component\HttpFoundation\JsonResponse;
1718
use Symfony\Component\HttpFoundation\Request;
@@ -40,10 +41,8 @@ public function new(Request $request, TaskService $taskService): Response
4041
if ($form->isSubmitted() && $form->isValid()) {
4142
/** @var TodoList $list */
4243
$list = $form->getData();
43-
44-
foreach ($list->getTasks() as $task) {
45-
$this->checkForGitHub($task, $taskService);
46-
}
44+
// @TODO Fix this for new tasks...
45+
$this->checkForGitHub($list->getTasks(), $taskService);
4746

4847
$this->listRepository->persist($list, true);
4948

@@ -57,15 +56,13 @@ public function new(Request $request, TaskService $taskService): Response
5756

5857
#[IsGranted(TodoListVoter::EDIT, 'todoList')]
5958
#[Route('/{id}/edit', name: 'list_edit', requirements: ['id' => Requirement::UUID], methods: ['GET', 'POST'])]
60-
public function edit(Request $request, TodoList $todoList, TaskService $taskService): Response
59+
public function edit(Request $request, TodoList $todoList, TaskService $taskService, TaskRepository $taskRepository): Response
6160
{
6261
$form = $this->createForm(TodoListType::class, $todoList);
6362
$form->handleRequest($request);
6463

6564
if ($form->isSubmitted() && $form->isValid()) {
66-
foreach ($todoList->getTasks() as $task) {
67-
$this->checkForGitHub($task, $taskService);
68-
}
65+
$todoList->setTasks($this->checkForGitHub($todoList->getTasks(), $taskService));
6966

7067
$this->listRepository->flush();
7168

@@ -105,24 +102,42 @@ public function removeTask(TodoList $todoList, Task $task): JsonResponse
105102
return $this->json(['Ok']);
106103
}
107104

108-
protected function checkForGitHub(Task $task, TaskService $taskService): void
105+
/**
106+
* @param Collection<int, Task> $tasks
107+
*
108+
* @return Collection<int, Task>
109+
*/
110+
protected function checkForGitHub(Collection $tasks, TaskService $taskService): Collection
109111
{
110-
$gitHubLink = UrlParser::getGitHubUrlFromText($task->getName());
112+
$links = [];
113+
114+
foreach ($tasks as $task) {
115+
$link = UrlParser::getGitHubUrlFromText($task->getName());
111116

112-
if (false === $gitHubLink) {
113-
return;
117+
if (!$link instanceof GitHubUrlData) {
118+
continue;
119+
}
120+
121+
$links[] = $link;
114122
}
115123

116124
try {
117125
// We want to query GitHub to grab the information about the link
118-
$data = $taskService->getGitHubDataFromUrl($gitHubLink);
119-
} catch (HttpClientException) {
120-
// @TODO - Do something with this in the future...
121-
return;
126+
$objects = $taskService->getGitHubDataFromUrl($links, 'rushlow-development', 'big-desk');
127+
} catch (HttpClientException $e) {
128+
throw new \RuntimeException('getGitHubDataFromUrl() Failed.', previous: $e);
122129
}
123130

124-
if ($data instanceof GitHubIssue || $data instanceof GitHubPullRequest) {
125-
$task->addGitHub($data);
131+
foreach ($objects as $gitHubObject) {
132+
$tasks = $tasks->map(function (Task $task) use ($gitHubObject) {
133+
if (str_contains($task->getName(), $gitHubObject->uri)) {
134+
$task->addGitHub($gitHubObject);
135+
}
136+
137+
return $task;
138+
});
126139
}
140+
141+
return $tasks;
127142
}
128143
}

src/Entity/TodoList.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@ public function addTask(Task $task): self
5555
return $this;
5656
}
5757

58+
/** @param Collection<int, Task> $tasks */
59+
public function setTasks(Collection $tasks): self
60+
{
61+
$this->tasks = $tasks;
62+
63+
return $this;
64+
}
65+
5866
public function removeTask(Task $task): self
5967
{
6068
$this->tasks->removeElement($task);

src/Factory/TodoListFactory.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Factory;
44

55
use App\Entity\TodoList;
6+
use App\Entity\User;
67
use App\Repository\TodoListRepository;
78
use Zenstruck\Foundry\ModelFactory;
89
use Zenstruck\Foundry\Proxy;
@@ -54,6 +55,12 @@ protected function getDefaults(): array
5455
];
5556
}
5657

58+
/** @param Proxy<User> $owner */
59+
public function withOwner(Proxy $owner): self
60+
{
61+
return $this->addState(['owner' => $owner]);
62+
}
63+
5764
#[\Override]
5865
protected static function getClass(): string
5966
{

0 commit comments

Comments
 (0)