Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Workflow][Registry] Added a new 'all' method #26656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/Symfony/Component/Workflow/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ CHANGELOG
* Added TransitionBlockers as a way to pass around reasons why exactly
transitions can't be made.
* Added a `MetadataStore`.
* Added `Registry::all` to return all the workflows associated with the
specific subject.

4.0.0
-----
Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/Workflow/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,23 @@ public function get($subject, $workflowName = null)
return $matched;
}

/**
* @param object $subject
*
* @return Workflow[]
*/
public function all($subject): array
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I'm not really sure about this name. You don't return all Workflows, but only workflows for a specific subject.
Maybe filterBySubject is a better name? What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chose all because the other function is called get (and not getBySubject).
BTW I think that filterBySubject is fine as well and it better explains what the function does.

Let me know if I have to change the name of the function, so I'll submit it together with the updated CHANGELOG.md file.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right about the get. Let's keep all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about allBySubject() then?

{
$matched = array();
foreach ($this->workflows as list($workflow, $supportStrategy)) {
if ($supportStrategy->supports($workflow, $subject)) {
$matched[] = $workflow;
}
}

return $matched;
}

private function supports(WorkflowInterface $workflow, $supportStrategy, $subject, $workflowName): bool
{
if (null !== $workflowName && $workflowName !== $workflow->getName()) {
Expand Down
27 changes: 27 additions & 0 deletions src/Symfony/Component/Workflow/Tests/RegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,33 @@ public function testGetWithNoMatch()
$this->assertSame('workflow1', $w1->getName());
}

public function testAllWithOneMatchWithSuccess()
{
$workflows = $this->registry->all(new Subject1());
$this->assertInternalType('array', $workflows);
$this->assertCount(1, $workflows);
$this->assertInstanceOf(Workflow::class, $workflows[0]);
$this->assertSame('workflow1', $workflows[0]->getName());
}

public function testAllWithMultipleMatchWithSuccess()
{
$workflows = $this->registry->all(new Subject2());
$this->assertInternalType('array', $workflows);
$this->assertCount(2, $workflows);
$this->assertInstanceOf(Workflow::class, $workflows[0]);
$this->assertInstanceOf(Workflow::class, $workflows[1]);
$this->assertSame('workflow2', $workflows[0]->getName());
$this->assertSame('workflow3', $workflows[1]->getName());
}

public function testAllWithNoMatch()
{
$workflows = $this->registry->all(new \stdClass());
$this->assertInternalType('array', $workflows);
$this->assertCount(0, $workflows);
}

/**
* @group legacy
*/
Expand Down