Skip to content

Commit

Permalink
#181: Fixed dataProvider fetch when clearing entity manager during sy…
Browse files Browse the repository at this point in the history
…nc processes
  • Loading branch information
tuj committed Jan 5, 2024
1 parent 1c7de6e commit 8af4a0b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Command/SyncIssuesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$dataProviders = $this->dataProviderRepository->findAll();

foreach ($dataProviders as $dataProvider) {
$projects = $this->projectRepository->findBy(['include' => true, 'dataProviderId' => $dataProvider->getId()]);
$projects = $this->projectRepository->findBy(['include' => true, 'dataProvider' => $dataProvider]);

$numberOfProjects = count($projects);

Expand Down
2 changes: 1 addition & 1 deletion src/Command/SyncWorklogsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$dataProviders = $this->dataProviderRepository->findAll();

foreach ($dataProviders as $dataProvider) {
$projects = $this->projectRepository->findBy(['include' => true, 'dataProviderId' => $dataProvider->getId()]);
$projects = $this->projectRepository->findBy(['include' => true, 'dataProvider' => $dataProvider]);

$numberOfProjects = count($projects);

Expand Down
20 changes: 18 additions & 2 deletions src/Service/DataSynchronizationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use App\Exception\UnsupportedDataProviderException;
use App\Repository\AccountRepository;
use App\Repository\ClientRepository;
use App\Repository\DataProviderRepository;
use App\Repository\InvoiceRepository;
use App\Repository\IssueRepository;
use App\Repository\ProjectRepository;
Expand All @@ -38,6 +39,7 @@ public function __construct(
private readonly AccountRepository $accountRepository,
private readonly InvoiceRepository $invoiceRepository,
private readonly DataProviderService $dataProviderService,
private readonly DataProviderRepository $dataProviderRepository,
) {
}

Expand All @@ -48,13 +50,16 @@ public function __construct(
*/
public function syncProjects(callable $progressCallback, DataProvider $dataProvider): void
{
$dataProviderId = $dataProvider->getId();

$service = $this->dataProviderService->getService($dataProvider);

// Get all projects from ApiService.
$allProjectData = $service->getAllProjectData();

foreach ($allProjectData as $index => $projectDatum) {
$project = $this->projectRepository->findOneBy(['projectTrackerId' => $projectDatum->projectTrackerId, 'dataProvider' => $dataProvider]);
$dataProvider = $this->dataProviderRepository->find($dataProviderId);

if (!$project) {
$project = new Project();
Expand Down Expand Up @@ -82,7 +87,7 @@ public function syncProjects(callable $progressCallback, DataProvider $dataProvi
}

// Only synchronize clients if this is enabled.
if ($dataProvider->isEnableClientSync()) {
if (null != $dataProvider && $dataProvider->isEnableClientSync()) {
$projectClientData = $service->getClientDataForProject($projectDatum->projectTrackerId);

foreach ($projectClientData as $clientData) {
Expand Down Expand Up @@ -133,6 +138,8 @@ public function syncProjects(callable $progressCallback, DataProvider $dataProvi
*/
public function syncAccounts(callable $progressCallback, DataProvider $dataProvider): void
{
$dataProviderId = $dataProvider->getId();

if ($dataProvider->isEnableAccountSync()) {
$service = $this->dataProviderService->getService($dataProvider);

Expand All @@ -144,6 +151,7 @@ public function syncAccounts(callable $progressCallback, DataProvider $dataProvi

if (!$account) {
$account = new Account();
$dataProvider = $this->dataProviderRepository->find($dataProviderId);
$account->setDataProvider($dataProvider);
$account->setProjectTrackerId($accountDatum->projectTrackerId);

Expand Down Expand Up @@ -177,6 +185,8 @@ public function syncAccounts(callable $progressCallback, DataProvider $dataProvi
*/
public function syncIssuesForProject(int $projectId, callable $progressCallback = null, DataProvider $dataProvider): void
{
$dataProviderId = $dataProvider->getId();

$service = $this->dataProviderService->getService($dataProvider);

$project = $this->projectRepository->find($projectId);
Expand All @@ -196,6 +206,7 @@ public function syncIssuesForProject(int $projectId, callable $progressCallback
$startAt = 0;

do {
$dataProvider = $this->dataProviderRepository->find($dataProviderId);
$project = $this->projectRepository->find($projectId);

if (!$project) {
Expand Down Expand Up @@ -260,6 +271,8 @@ public function syncIssuesForProject(int $projectId, callable $progressCallback
*/
public function syncWorklogsForProject(int $projectId, callable $progressCallback = null, DataProvider $dataProvider): void
{
$dataProviderId = $dataProvider->getId();

$service = $this->dataProviderService->getService($dataProvider);

$project = $this->projectRepository->find($projectId);
Expand Down Expand Up @@ -288,6 +301,9 @@ public function syncWorklogsForProject(int $projectId, callable $progressCallbac

if (!$worklog) {
$worklog = new Worklog();

$dataProvider = $this->dataProviderRepository->find($dataProviderId);

$worklog->setDataProvider($dataProvider);

$this->entityManager->persist($worklog);
Expand All @@ -300,7 +316,7 @@ public function syncWorklogsForProject(int $projectId, callable $progressCallbac
$worklog->setProjectTrackerIssueId($worklogDatum->projectTrackerIssueId);
$worklog->setTimeSpentSeconds($worklogDatum->timeSpentSeconds);

if (null !== $worklog->getProjectTrackerIssueId()) {
if (null != $worklog->getProjectTrackerIssueId()) {
$issue = $this->issueRepository->findOneBy(['projectTrackerId' => $worklog->getProjectTrackerIssueId()]);
$worklog->setIssue($issue);
}
Expand Down

0 comments on commit 8af4a0b

Please sign in to comment.