Skip to content

Implement a hash order number generator.#1159

Merged
pjedrzejewski merged 1 commit intoSylius:masterfrom
mykehsd:feature/I#922-OrderNumberGenerator
Mar 17, 2014
Merged

Implement a hash order number generator.#1159
pjedrzejewski merged 1 commit intoSylius:masterfrom
mykehsd:feature/I#922-OrderNumberGenerator

Conversation

@mykehsd
Copy link
Copy Markdown
Contributor

@mykehsd mykehsd commented Mar 4, 2014

Removes the autoincrementing id number that some e-tailers dont like.
Fixes #922

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is not the default one.

@Richtermeister
Copy link
Copy Markdown
Contributor

This generator does not guard against duplicate numbers being issued.

Edit: Good idea though, and very welcome contribution if you can take it a step further :)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's PHP 5.4 sugar, we support 5.3 too.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@stloyd Your right, thanks for the correction!

@pjedrzejewski
Copy link
Copy Markdown
Contributor

Yes, we should perform a check to avoid duplicate numbers. What do you think @mykehsd ?

@mykehsd
Copy link
Copy Markdown
Contributor Author

mykehsd commented Mar 7, 2014

@pjedrzejewski I don't really like the for() in this solution but we wouldn't want to indefinitely look for order numbers. Other ideas?

@Richtermeister
Copy link
Copy Markdown
Contributor

Try a do...while(). And for now I would not worry about the indefinite part, should not be a real-world issue, and if it becomes one, we can introduce a limit parameter later.

@mykehsd
Copy link
Copy Markdown
Contributor Author

mykehsd commented Mar 7, 2014

@Richtermeister That wouldn't be testable in phpspec afaik
What's the advantage over for() ?

@Richtermeister
Copy link
Copy Markdown
Contributor

Why would it be not testable? The advantage is that you don't just loop for an arbitrary number of times, as you do, but you loop until a condition is fulfilled.

@mykehsd
Copy link
Copy Markdown
Contributor Author

mykehsd commented Mar 7, 2014

The failure event wouldn't be testable since it will never exit that loop.

@Richtermeister
Copy link
Copy Markdown
Contributor

You mean because you can't tell phpspec mocks to return false once and true the second time? You actually can, by providing a custom handling function.

@mykehsd
Copy link
Copy Markdown
Contributor Author

mykehsd commented Mar 7, 2014

That would be a good addition but I'm talking about the issue where isUnused() never returns true

@Richtermeister
Copy link
Copy Markdown
Contributor

Why would it never return true? In reality we're just guarding against the occasional collision, unlikely to be more than one in a row, and even if there were to be many more, you should always get to a point where an unused number is found. Otherwise the problem is more with the hash generation strategy and the entire generator should be reworked.

@mykehsd
Copy link
Copy Markdown
Contributor Author

mykehsd commented Mar 7, 2014

In respect to

        $numberRepository->isUnused(Argument::any())->willReturn(false);

it would never return (infinite loop). In reality, the hashing strategy will very unlikely collide with an existing number but I think throwing an exception is better then an infinite loop

@Richtermeister
Copy link
Copy Markdown
Contributor

In general the spec would be sufficient to test that the method is just being called. You're not testing the repository, after all. If you really wanted to test against 2 executions of the loop, you can use something like this:

$counter = 0;
$numberRepository->isUnused(Argument::any())->willReturn(function() use (&$counter){
    $counter++;
    return $counter > 1;
});

@mykehsd
Copy link
Copy Markdown
Contributor Author

mykehsd commented Mar 7, 2014

@Richtermeister ok, do...while added. Thanks for your feedback!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

There's no need for the condition:

do {
    $number = $this->generateSegment(3) . '-' . $this->generateSegment(7) . '-' . $this->generateSegment(7));
} while (!$this->numberRepository->isUnused($number));

I would also delegate the format of the number generation to a separate method generateNumber() which can be overridden if somebody doesn't like the 3 segment approach.

Maybe we could also call the method numberExists() to be less double-thinky.

@mykehsd
Copy link
Copy Markdown
Contributor Author

mykehsd commented Mar 17, 2014

I don't know why Travis shows errored, but it shows passed

pjedrzejewski pushed a commit that referenced this pull request Mar 17, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Enhancement Minor issues and PRs improving the current solutions (optimizations, typo fixes, etc.).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unique Order Id's

5 participants