Implement a hash order number generator.#1159
Conversation
There was a problem hiding this comment.
This is not the default one.
|
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 :) |
There was a problem hiding this comment.
It's PHP 5.4 sugar, we support 5.3 too.
There was a problem hiding this comment.
@stloyd Your right, thanks for the correction!
|
Yes, we should perform a check to avoid duplicate numbers. What do you think @mykehsd ? |
|
@pjedrzejewski I don't really like the for() in this solution but we wouldn't want to indefinitely look for order numbers. Other ideas? |
|
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. |
|
@Richtermeister That wouldn't be testable in phpspec afaik |
|
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. |
|
The failure event wouldn't be testable since it will never exit that loop. |
|
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. |
|
That would be a good addition but I'm talking about the issue where isUnused() never returns true |
|
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. |
|
In respect to 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 |
|
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: |
|
@Richtermeister ok, do...while added. Thanks for your feedback! |
There was a problem hiding this comment.
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.
… id number that some e-tailers dont like. For Sylius#922
|
I don't know why Travis shows errored, but it shows passed |
Implement a hash order number generator.
Removes the autoincrementing id number that some e-tailers dont like.
Fixes #922